2007-06-02 02:02:12 +08:00
|
|
|
//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2007-06-20 12:44:43 +08:00
|
|
|
#include "CodeGenModule.h"
|
2007-06-02 02:02:12 +08:00
|
|
|
#include "clang/AST/AST.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2007-06-06 12:54:52 +08:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
Add a hack (mirroring llvm-gcc) to pointer difference
codegen to compile:
int test(int *A, int *B) {
return A-B;
}
into:
_test:
movl 4(%esp), %eax
subl 8(%esp), %eax
sarl $2, %eax
ret
instead of:
_test:
movl 4(%esp), %eax
subl 8(%esp), %eax
movl %eax, %ecx
sarl $31, %ecx
shrl $30, %ecx
addl %ecx, %eax
sarl $2, %eax
ret
llvm-svn: 39902
2007-07-16 13:43:05 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2007-06-02 02:02:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-06-03 03:33:17 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Miscellaneous Helper Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-06-23 05:44:33 +08:00
|
|
|
/// CreateTempAlloca - This creates a alloca and inserts it into the entry
|
|
|
|
/// block.
|
|
|
|
llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
|
|
|
|
const char *Name) {
|
|
|
|
return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
|
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
|
|
|
|
/// expression and compare the result against zero, returning an Int1Ty value.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
|
2007-08-09 01:43:05 +08:00
|
|
|
return ConvertScalarValueToBool(EmitExpr(E), E->getType());
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-06-23 05:44:33 +08:00
|
|
|
/// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
|
|
|
|
/// load the real and imaginary pieces, returning them as Real/Imag.
|
|
|
|
void CodeGenFunction::EmitLoadOfComplex(RValue V,
|
|
|
|
llvm::Value *&Real, llvm::Value *&Imag){
|
|
|
|
llvm::Value *Ptr = V.getAggregateAddr();
|
|
|
|
|
|
|
|
llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
|
|
|
|
llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
|
|
|
|
llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Zero, Zero, "realp");
|
|
|
|
llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Zero, One, "imagp");
|
|
|
|
|
|
|
|
// FIXME: Handle volatility.
|
|
|
|
Real = Builder.CreateLoad(RealPtr, "real");
|
|
|
|
Imag = Builder.CreateLoad(ImagPtr, "imag");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitStoreOfComplex - Store the specified real/imag parts into the
|
|
|
|
/// specified value pointer.
|
|
|
|
void CodeGenFunction::EmitStoreOfComplex(llvm::Value *Real, llvm::Value *Imag,
|
|
|
|
llvm::Value *ResPtr) {
|
|
|
|
llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
|
|
|
|
llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
|
|
|
|
llvm::Value *RealPtr = Builder.CreateGEP(ResPtr, Zero, Zero, "real");
|
|
|
|
llvm::Value *ImagPtr = Builder.CreateGEP(ResPtr, Zero, One, "imag");
|
|
|
|
|
|
|
|
// FIXME: Handle volatility.
|
|
|
|
Builder.CreateStore(Real, RealPtr);
|
|
|
|
Builder.CreateStore(Imag, ImagPtr);
|
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Conversions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// EmitConversion - Convert the value specied by Val, whose type is ValTy, to
|
|
|
|
/// the type specified by DstTy, following the rules of C99 6.3.
|
|
|
|
RValue CodeGenFunction::EmitConversion(RValue Val, QualType ValTy,
|
2007-06-23 03:05:19 +08:00
|
|
|
QualType DstTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
ValTy = ValTy.getCanonicalType();
|
|
|
|
DstTy = DstTy.getCanonicalType();
|
|
|
|
if (ValTy == DstTy) return Val;
|
2007-06-06 12:39:08 +08:00
|
|
|
|
|
|
|
// Handle conversions to bool first, they are special: comparisons against 0.
|
|
|
|
if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstTy))
|
|
|
|
if (DestBT->getKind() == BuiltinType::Bool)
|
|
|
|
return RValue::get(ConvertScalarValueToBool(Val, ValTy));
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-06-06 12:39:08 +08:00
|
|
|
// Handle pointer conversions next: pointers can only be converted to/from
|
|
|
|
// other pointers and integers.
|
2007-06-06 12:05:39 +08:00
|
|
|
if (isa<PointerType>(DstTy)) {
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *DestTy = ConvertType(DstTy);
|
2007-06-06 12:05:39 +08:00
|
|
|
|
2007-08-11 00:33:59 +08:00
|
|
|
if (Val.getVal()->getType() == DestTy)
|
|
|
|
return Val;
|
|
|
|
|
2007-06-06 12:05:39 +08:00
|
|
|
// The source value may be an integer, or a pointer.
|
|
|
|
assert(Val.isScalar() && "Can only convert from integer or pointer");
|
|
|
|
if (isa<llvm::PointerType>(Val.getVal()->getType()))
|
|
|
|
return RValue::get(Builder.CreateBitCast(Val.getVal(), DestTy, "conv"));
|
|
|
|
assert(ValTy->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
|
2007-07-13 11:25:53 +08:00
|
|
|
return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv"));
|
2007-06-06 12:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<PointerType>(ValTy)) {
|
2007-06-06 12:05:39 +08:00
|
|
|
// Must be an ptr to int cast.
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *DestTy = ConvertType(DstTy);
|
2007-06-06 12:05:39 +08:00
|
|
|
assert(isa<llvm::IntegerType>(DestTy) && "not ptr->int?");
|
|
|
|
return RValue::get(Builder.CreateIntToPtr(Val.getVal(), DestTy, "conv"));
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
2007-06-06 12:39:08 +08:00
|
|
|
|
|
|
|
// Finally, we have the arithmetic types: real int/float and complex
|
|
|
|
// int/float. Handle real->real conversions first, they are the most
|
|
|
|
// common.
|
|
|
|
if (Val.isScalar() && DstTy->isRealType()) {
|
|
|
|
// We know that these are representable as scalars in LLVM, convert to LLVM
|
|
|
|
// types since they are easier to reason about.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *SrcVal = Val.getVal();
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *DestTy = ConvertType(DstTy);
|
2007-06-06 12:39:08 +08:00
|
|
|
if (SrcVal->getType() == DestTy) return Val;
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *Result;
|
2007-06-06 12:39:08 +08:00
|
|
|
if (isa<llvm::IntegerType>(SrcVal->getType())) {
|
|
|
|
bool InputSigned = ValTy->isSignedIntegerType();
|
|
|
|
if (isa<llvm::IntegerType>(DestTy))
|
|
|
|
Result = Builder.CreateIntCast(SrcVal, DestTy, InputSigned, "conv");
|
|
|
|
else if (InputSigned)
|
|
|
|
Result = Builder.CreateSIToFP(SrcVal, DestTy, "conv");
|
|
|
|
else
|
|
|
|
Result = Builder.CreateUIToFP(SrcVal, DestTy, "conv");
|
|
|
|
} else {
|
|
|
|
assert(SrcVal->getType()->isFloatingPoint() && "Unknown real conversion");
|
|
|
|
if (isa<llvm::IntegerType>(DestTy)) {
|
|
|
|
if (DstTy->isSignedIntegerType())
|
|
|
|
Result = Builder.CreateFPToSI(SrcVal, DestTy, "conv");
|
|
|
|
else
|
|
|
|
Result = Builder.CreateFPToUI(SrcVal, DestTy, "conv");
|
|
|
|
} else {
|
|
|
|
assert(DestTy->isFloatingPoint() && "Unknown real conversion");
|
|
|
|
if (DestTy->getTypeID() < SrcVal->getType()->getTypeID())
|
|
|
|
Result = Builder.CreateFPTrunc(SrcVal, DestTy, "conv");
|
|
|
|
else
|
|
|
|
Result = Builder.CreateFPExt(SrcVal, DestTy, "conv");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RValue::get(Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0 && "FIXME: We don't support complex conversions yet!");
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ConvertScalarValueToBool - Convert the specified expression value to a
|
2007-06-03 03:33:17 +08:00
|
|
|
/// boolean (i1) truth value. This is equivalent to "Val == 0".
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *CodeGenFunction::ConvertScalarValueToBool(RValue Val, QualType Ty){
|
2007-06-03 03:33:17 +08:00
|
|
|
Ty = Ty.getCanonicalType();
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *Result;
|
2007-06-03 03:33:17 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(Ty)) {
|
|
|
|
switch (BT->getKind()) {
|
|
|
|
default: assert(0 && "Unknown scalar value");
|
|
|
|
case BuiltinType::Bool:
|
|
|
|
Result = Val.getVal();
|
|
|
|
// Bool is already evaluated right.
|
|
|
|
assert(Result->getType() == llvm::Type::Int1Ty &&
|
|
|
|
"Unexpected bool value type!");
|
|
|
|
return Result;
|
2007-06-03 15:25:34 +08:00
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::Char_U:
|
2007-06-03 03:33:17 +08:00
|
|
|
case BuiltinType::SChar:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
case BuiltinType::Short:
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
case BuiltinType::Int:
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
case BuiltinType::Long:
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
case BuiltinType::ULongLong:
|
|
|
|
// Code below handles simple integers.
|
|
|
|
break;
|
|
|
|
case BuiltinType::Float:
|
|
|
|
case BuiltinType::Double:
|
|
|
|
case BuiltinType::LongDouble: {
|
|
|
|
// Compare against 0.0 for fp scalars.
|
|
|
|
Result = Val.getVal();
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
|
2007-06-03 03:33:17 +08:00
|
|
|
// FIXME: llvm-gcc produces a une comparison: validate this is right.
|
|
|
|
Result = Builder.CreateFCmpUNE(Result, Zero, "tobool");
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
}
|
2007-06-23 04:56:16 +08:00
|
|
|
} else if (isa<PointerType>(Ty) ||
|
|
|
|
cast<TagType>(Ty)->getDecl()->getKind() == Decl::Enum) {
|
2007-06-03 03:33:17 +08:00
|
|
|
// Code below handles this fine.
|
2007-06-23 04:56:16 +08:00
|
|
|
} else {
|
|
|
|
assert(isa<ComplexType>(Ty) && "Unknwon type!");
|
|
|
|
assert(0 && "FIXME: comparisons against complex not implemented yet");
|
2007-06-03 03:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Usual case for integers, pointers, and enums: compare against zero.
|
|
|
|
Result = Val.getVal();
|
2007-06-03 03:47:04 +08:00
|
|
|
|
|
|
|
// Because of the type rules of C, we often end up computing a logical value,
|
|
|
|
// then zero extending it to int, then wanting it as a logical value again.
|
|
|
|
// Optimize this common case.
|
2007-06-16 07:05:46 +08:00
|
|
|
if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Result)) {
|
2007-06-03 03:47:04 +08:00
|
|
|
if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
|
|
|
|
Result = ZI->getOperand(0);
|
|
|
|
ZI->eraseFromParent();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(Result->getType());
|
2007-06-03 03:33:17 +08:00
|
|
|
return Builder.CreateICmpNE(Result, Zero, "tobool");
|
|
|
|
}
|
|
|
|
|
2007-06-03 03:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-02 13:24:33 +08:00
|
|
|
// LValue Expression Emission
|
2007-06-03 03:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-02 13:24:33 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EmitLValue - Emit code to compute a designator that specifies the location
|
|
|
|
/// of the expression.
|
|
|
|
///
|
|
|
|
/// This can return one of two things: a simple address or a bitfield
|
|
|
|
/// reference. In either case, the LLVM Value* in the LValue structure is
|
|
|
|
/// guaranteed to be an LLVM pointer type.
|
|
|
|
///
|
|
|
|
/// If this returns a bitfield reference, nothing about the pointee type of
|
|
|
|
/// the LLVM value is known: For example, it may not be a pointer to an
|
|
|
|
/// integer.
|
|
|
|
///
|
|
|
|
/// If this returns a normal address, and if the lvalue's C type is fixed
|
|
|
|
/// size, this method guarantees that the returned pointer type will point to
|
|
|
|
/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
|
|
|
|
/// variable length type, this is not possible.
|
|
|
|
///
|
2007-06-02 13:24:33 +08:00
|
|
|
LValue CodeGenFunction::EmitLValue(const Expr *E) {
|
|
|
|
switch (E->getStmtClass()) {
|
|
|
|
default:
|
2007-06-06 04:53:16 +08:00
|
|
|
fprintf(stderr, "Unimplemented lvalue expr!\n");
|
2007-06-02 13:24:33 +08:00
|
|
|
E->dump();
|
2007-07-11 05:17:59 +08:00
|
|
|
return LValue::MakeAddr(llvm::UndefValue::get(
|
2007-06-02 13:24:33 +08:00
|
|
|
llvm::PointerType::get(llvm::Type::Int32Ty)));
|
|
|
|
|
|
|
|
case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
|
2007-06-05 11:59:43 +08:00
|
|
|
case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
|
2007-07-21 13:21:51 +08:00
|
|
|
case Expr::PreDefinedExprClass:
|
|
|
|
return EmitPreDefinedLValue(cast<PreDefinedExpr>(E));
|
2007-06-06 12:54:52 +08:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return EmitStringLiteralLValue(cast<StringLiteral>(E));
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
case Expr::UnaryOperatorClass:
|
|
|
|
return EmitUnaryOpLValue(cast<UnaryOperator>(E));
|
2007-06-09 07:31:14 +08:00
|
|
|
case Expr::ArraySubscriptExprClass:
|
|
|
|
return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
|
2007-08-04 01:31:20 +08:00
|
|
|
case Expr::OCUVectorElementExprClass:
|
|
|
|
return EmitOCUVectorElementExpr(cast<OCUVectorElementExpr>(E));
|
2007-06-02 13:24:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
|
|
|
|
/// this method emits the address of the lvalue, then loads the result as an
|
|
|
|
/// rvalue, returning the rvalue.
|
2007-06-30 00:31:29 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
|
2007-07-11 05:17:59 +08:00
|
|
|
if (LV.isSimple()) {
|
|
|
|
llvm::Value *Ptr = LV.getAddress();
|
|
|
|
const llvm::Type *EltTy =
|
|
|
|
cast<llvm::PointerType>(Ptr->getType())->getElementType();
|
|
|
|
|
|
|
|
// Simple scalar l-value.
|
|
|
|
if (EltTy->isFirstClassType())
|
|
|
|
return RValue::get(Builder.CreateLoad(Ptr, "tmp"));
|
|
|
|
|
2007-08-11 00:33:59 +08:00
|
|
|
if (ExprType->isFunctionType())
|
|
|
|
return RValue::get(Ptr);
|
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
// Otherwise, we have an aggregate lvalue.
|
|
|
|
return RValue::getAggregate(Ptr);
|
|
|
|
}
|
2007-06-23 02:48:09 +08:00
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
if (LV.isVectorElt()) {
|
|
|
|
llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(), "tmp");
|
|
|
|
return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
|
|
|
|
"vecext"));
|
|
|
|
}
|
implement lvalue to rvalue conversion for ocuvector components. We can now compile stuff
like this:
typedef __attribute__(( ocu_vector_type(4) )) float float4;
float4 test1(float4 V) {
return V.wzyx+V;
}
to:
_test1:
pshufd $27, %xmm0, %xmm1
addps %xmm0, %xmm1
movaps %xmm1, %xmm0
ret
and:
_test1:
mfspr r2, 256
oris r3, r2, 4096
mtspr 256, r3
li r3, lo16(LCPI1_0)
lis r4, ha16(LCPI1_0)
lvx v3, r4, r3
vperm v3, v2, v2, v3
vaddfp v2, v3, v2
mtspr 256, r2
blr
llvm-svn: 40771
2007-08-03 08:16:29 +08:00
|
|
|
|
|
|
|
// If this is a reference to a subset of the elements of a vector, either
|
|
|
|
// shuffle the input or extract/insert them as appropriate.
|
2007-08-04 01:31:20 +08:00
|
|
|
if (LV.isOCUVectorElt())
|
|
|
|
return EmitLoadOfOCUElementLValue(LV, ExprType);
|
2007-08-04 00:18:34 +08:00
|
|
|
|
|
|
|
assert(0 && "Bitfield ref not impl!");
|
|
|
|
}
|
2007-08-03 23:52:31 +08:00
|
|
|
|
2007-08-04 00:18:34 +08:00
|
|
|
// If this is a reference to a subset of the elements of a vector, either
|
|
|
|
// shuffle the input or extract/insert them as appropriate.
|
2007-08-04 01:31:20 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfOCUElementLValue(LValue LV,
|
2007-08-04 00:18:34 +08:00
|
|
|
QualType ExprType) {
|
|
|
|
llvm::Value *Vec = Builder.CreateLoad(LV.getOCUVectorAddr(), "tmp");
|
|
|
|
|
2007-08-04 01:31:20 +08:00
|
|
|
unsigned EncFields = LV.getOCUVectorElts();
|
2007-08-04 00:18:34 +08:00
|
|
|
|
|
|
|
// If the result of the expression is a non-vector type, we must be
|
|
|
|
// extracting a single element. Just codegen as an extractelement.
|
|
|
|
if (!isa<VectorType>(ExprType)) {
|
2007-08-04 01:31:20 +08:00
|
|
|
unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
|
2007-08-04 00:18:34 +08:00
|
|
|
llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
|
|
|
|
return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the source and destination have the same number of elements, use a
|
|
|
|
// vector shuffle instead of insert/extracts.
|
|
|
|
unsigned NumResultElts = cast<VectorType>(ExprType)->getNumElements();
|
|
|
|
unsigned NumSourceElts =
|
|
|
|
cast<llvm::VectorType>(Vec->getType())->getNumElements();
|
|
|
|
|
|
|
|
if (NumResultElts == NumSourceElts) {
|
|
|
|
llvm::SmallVector<llvm::Constant*, 4> Mask;
|
In the common case where we are shuffling a vector, emit an
llvm vector shuffle instead of a bunch of insert/extract operations.
For: vec4 = vec4.yyyy; // splat
Emit:
%tmp1 = shufflevector <4 x float> %tmp, <4 x float> undef, <4 x i32> < i32 1, i32 1, i32 1, i32 1 >
instead of:
%tmp1 = extractelement <4 x float> %tmp, i32 1
%tmp2 = insertelement <4 x float> undef, float %tmp1, i32 0
%tmp3 = extractelement <4 x float> %tmp, i32 1
%tmp4 = insertelement <4 x float> %tmp2, float %tmp3, i32 1
%tmp5 = extractelement <4 x float> %tmp, i32 1
%tmp6 = insertelement <4 x float> %tmp4, float %tmp5, i32 2
%tmp7 = extractelement <4 x float> %tmp, i32 1
%tmp8 = insertelement <4 x float> %tmp6, float %tmp7, i32 3
llvm-svn: 40779
2007-08-04 00:09:33 +08:00
|
|
|
for (unsigned i = 0; i != NumResultElts; ++i) {
|
2007-08-04 01:31:20 +08:00
|
|
|
unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
|
2007-08-04 00:18:34 +08:00
|
|
|
Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
|
implement lvalue to rvalue conversion for ocuvector components. We can now compile stuff
like this:
typedef __attribute__(( ocu_vector_type(4) )) float float4;
float4 test1(float4 V) {
return V.wzyx+V;
}
to:
_test1:
pshufd $27, %xmm0, %xmm1
addps %xmm0, %xmm1
movaps %xmm1, %xmm0
ret
and:
_test1:
mfspr r2, 256
oris r3, r2, 4096
mtspr 256, r3
li r3, lo16(LCPI1_0)
lis r4, ha16(LCPI1_0)
lvx v3, r4, r3
vperm v3, v2, v2, v3
vaddfp v2, v3, v2
mtspr 256, r2
blr
llvm-svn: 40771
2007-08-03 08:16:29 +08:00
|
|
|
}
|
|
|
|
|
2007-08-04 00:18:34 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
|
|
|
|
Vec = Builder.CreateShuffleVector(Vec,
|
|
|
|
llvm::UndefValue::get(Vec->getType()),
|
|
|
|
MaskV, "tmp");
|
|
|
|
return RValue::get(Vec);
|
implement lvalue to rvalue conversion for ocuvector components. We can now compile stuff
like this:
typedef __attribute__(( ocu_vector_type(4) )) float float4;
float4 test1(float4 V) {
return V.wzyx+V;
}
to:
_test1:
pshufd $27, %xmm0, %xmm1
addps %xmm0, %xmm1
movaps %xmm1, %xmm0
ret
and:
_test1:
mfspr r2, 256
oris r3, r2, 4096
mtspr 256, r3
li r3, lo16(LCPI1_0)
lis r4, ha16(LCPI1_0)
lvx v3, r4, r3
vperm v3, v2, v2, v3
vaddfp v2, v3, v2
mtspr 256, r2
blr
llvm-svn: 40771
2007-08-03 08:16:29 +08:00
|
|
|
}
|
2007-06-23 02:48:09 +08:00
|
|
|
|
2007-08-04 00:18:34 +08:00
|
|
|
// Start out with an undef of the result type.
|
|
|
|
llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType));
|
|
|
|
|
|
|
|
// Extract/Insert each element of the result.
|
|
|
|
for (unsigned i = 0; i != NumResultElts; ++i) {
|
2007-08-04 01:31:20 +08:00
|
|
|
unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
|
2007-08-04 00:18:34 +08:00
|
|
|
llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
|
|
|
|
Elt = Builder.CreateExtractElement(Vec, Elt, "tmp");
|
|
|
|
|
|
|
|
llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
|
|
|
|
Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp");
|
|
|
|
}
|
|
|
|
|
|
|
|
return RValue::get(Result);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-08-04 00:18:34 +08:00
|
|
|
|
2007-06-30 00:31:29 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfLValue(const Expr *E) {
|
|
|
|
return EmitLoadOfLValue(EmitLValue(E), E->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EmitStoreThroughLValue - Store the specified rvalue into the specified
|
|
|
|
/// lvalue, where both are guaranteed to the have the same type, and that type
|
|
|
|
/// is 'Ty'.
|
|
|
|
void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
|
|
|
|
QualType Ty) {
|
2007-08-04 00:28:33 +08:00
|
|
|
if (!Dst.isSimple()) {
|
|
|
|
if (Dst.isVectorElt()) {
|
|
|
|
// Read/modify/write the vector, inserting the new element.
|
|
|
|
// FIXME: Volatility.
|
|
|
|
llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(), "tmp");
|
|
|
|
Vec = Builder.CreateInsertElement(Vec, Src.getVal(),
|
|
|
|
Dst.getVectorIdx(), "vecins");
|
|
|
|
Builder.CreateStore(Vec, Dst.getVectorAddr());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is an update of elements of a vector, insert them as appropriate.
|
2007-08-04 01:31:20 +08:00
|
|
|
if (Dst.isOCUVectorElt())
|
2007-08-04 00:28:33 +08:00
|
|
|
return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty);
|
2007-07-11 05:17:59 +08:00
|
|
|
|
2007-08-04 00:28:33 +08:00
|
|
|
assert(0 && "FIXME: Don't support store to bitfield yet");
|
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-06-23 02:48:09 +08:00
|
|
|
llvm::Value *DstAddr = Dst.getAddress();
|
|
|
|
if (Src.isScalar()) {
|
|
|
|
// FIXME: Handle volatility etc.
|
|
|
|
const llvm::Type *SrcTy = Src.getVal()->getType();
|
|
|
|
const llvm::Type *AddrTy =
|
|
|
|
cast<llvm::PointerType>(DstAddr->getType())->getElementType();
|
|
|
|
|
|
|
|
if (AddrTy != SrcTy)
|
|
|
|
DstAddr = Builder.CreateBitCast(DstAddr, llvm::PointerType::get(SrcTy),
|
|
|
|
"storetmp");
|
|
|
|
Builder.CreateStore(Src.getVal(), DstAddr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-23 05:44:33 +08:00
|
|
|
// Don't use memcpy for complex numbers.
|
|
|
|
if (Ty->isComplexType()) {
|
|
|
|
llvm::Value *Real, *Imag;
|
|
|
|
EmitLoadOfComplex(Src, Real, Imag);
|
|
|
|
EmitStoreOfComplex(Real, Imag, Dst.getAddress());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-23 02:48:09 +08:00
|
|
|
// Aggregate assignment turns into llvm.memcpy.
|
|
|
|
const llvm::Type *SBP = llvm::PointerType::get(llvm::Type::Int8Ty);
|
|
|
|
llvm::Value *SrcAddr = Src.getAggregateAddr();
|
|
|
|
|
|
|
|
if (DstAddr->getType() != SBP)
|
|
|
|
DstAddr = Builder.CreateBitCast(DstAddr, SBP, "tmp");
|
|
|
|
if (SrcAddr->getType() != SBP)
|
|
|
|
SrcAddr = Builder.CreateBitCast(SrcAddr, SBP, "tmp");
|
|
|
|
|
|
|
|
unsigned Align = 1; // FIXME: Compute type alignments.
|
|
|
|
unsigned Size = 1234; // FIXME: Compute type sizes.
|
|
|
|
|
|
|
|
// FIXME: Handle variable sized types.
|
|
|
|
const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
|
|
|
|
llvm::Value *SizeVal = llvm::ConstantInt::get(IntPtr, Size);
|
|
|
|
|
|
|
|
llvm::Value *MemCpyOps[4] = {
|
|
|
|
DstAddr, SrcAddr, SizeVal,llvm::ConstantInt::get(llvm::Type::Int32Ty, Align)
|
|
|
|
};
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-08-01 14:24:52 +08:00
|
|
|
Builder.CreateCall(CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-08-04 00:28:33 +08:00
|
|
|
void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst,
|
|
|
|
QualType Ty) {
|
|
|
|
// This access turns into a read/modify/write of the vector. Load the input
|
|
|
|
// value now.
|
|
|
|
llvm::Value *Vec = Builder.CreateLoad(Dst.getOCUVectorAddr(), "tmp");
|
|
|
|
// FIXME: Volatility.
|
2007-08-04 01:31:20 +08:00
|
|
|
unsigned EncFields = Dst.getOCUVectorElts();
|
2007-08-04 00:28:33 +08:00
|
|
|
|
|
|
|
llvm::Value *SrcVal = Src.getVal();
|
|
|
|
|
2007-08-04 00:37:04 +08:00
|
|
|
if (const VectorType *VTy = Ty->getAsVectorType()) {
|
|
|
|
unsigned NumSrcElts = VTy->getNumElements();
|
|
|
|
|
|
|
|
// Extract/Insert each element.
|
|
|
|
for (unsigned i = 0; i != NumSrcElts; ++i) {
|
|
|
|
llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
|
|
|
|
Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp");
|
|
|
|
|
2007-08-04 01:31:20 +08:00
|
|
|
unsigned Idx = OCUVectorElementExpr::getAccessedFieldNo(i, EncFields);
|
2007-08-04 00:37:04 +08:00
|
|
|
llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx);
|
|
|
|
Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the Src is a scalar (not a vector) it must be updating one element.
|
2007-08-04 01:31:20 +08:00
|
|
|
unsigned InIdx = OCUVectorElementExpr::getAccessedFieldNo(0, EncFields);
|
2007-08-04 00:28:33 +08:00
|
|
|
llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
|
|
|
|
Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
|
|
|
|
}
|
|
|
|
|
|
|
|
Builder.CreateStore(Vec, Dst.getOCUVectorAddr());
|
|
|
|
}
|
|
|
|
|
2007-06-02 13:24:33 +08:00
|
|
|
|
|
|
|
LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
|
|
|
|
const Decl *D = E->getDecl();
|
2007-06-14 04:44:40 +08:00
|
|
|
if (isa<BlockVarDecl>(D) || isa<ParmVarDecl>(D)) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *V = LocalDeclMap[D];
|
2007-06-02 13:24:33 +08:00
|
|
|
assert(V && "BlockVarDecl not entered in LocalDeclMap?");
|
2007-07-11 05:17:59 +08:00
|
|
|
return LValue::MakeAddr(V);
|
2007-06-20 12:44:43 +08:00
|
|
|
} else if (isa<FunctionDecl>(D) || isa<FileVarDecl>(D)) {
|
2007-07-11 05:17:59 +08:00
|
|
|
return LValue::MakeAddr(CGM.GetAddrOfGlobalDecl(D));
|
2007-06-02 13:24:33 +08:00
|
|
|
}
|
|
|
|
assert(0 && "Unimp declref");
|
|
|
|
}
|
2007-06-02 02:02:12 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
|
|
|
|
// __extension__ doesn't affect lvalue-ness.
|
|
|
|
if (E->getOpcode() == UnaryOperator::Extension)
|
|
|
|
return EmitLValue(E->getSubExpr());
|
|
|
|
|
|
|
|
assert(E->getOpcode() == UnaryOperator::Deref &&
|
|
|
|
"'*' is the only unary operator that produces an lvalue");
|
2007-07-11 05:17:59 +08:00
|
|
|
return LValue::MakeAddr(EmitExpr(E->getSubExpr()).getVal());
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-06-06 12:54:52 +08:00
|
|
|
LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
|
|
|
|
assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
|
|
|
|
const char *StrData = E->getStrData();
|
|
|
|
unsigned Len = E->getByteLength();
|
|
|
|
|
|
|
|
// FIXME: Can cache/reuse these within the module.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Constant *C=llvm::ConstantArray::get(std::string(StrData, StrData+Len));
|
2007-06-06 12:54:52 +08:00
|
|
|
|
|
|
|
// Create a global variable for this.
|
2007-06-16 07:05:46 +08:00
|
|
|
C = new llvm::GlobalVariable(C->getType(), true,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2007-06-06 12:54:52 +08:00
|
|
|
C, ".str", CurFn->getParent());
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
|
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
|
|
|
C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
|
2007-07-11 05:17:59 +08:00
|
|
|
return LValue::MakeAddr(C);
|
2007-06-06 12:54:52 +08:00
|
|
|
}
|
|
|
|
|
2007-07-21 13:21:51 +08:00
|
|
|
LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
|
|
|
|
std::string FunctionName(CurFuncDecl->getName());
|
|
|
|
std::string GlobalVarName;
|
|
|
|
|
|
|
|
switch (E->getIdentType()) {
|
|
|
|
default:
|
|
|
|
assert(0 && "unknown pre-defined ident type");
|
|
|
|
case PreDefinedExpr::Func:
|
|
|
|
GlobalVarName = "__func__.";
|
|
|
|
break;
|
|
|
|
case PreDefinedExpr::Function:
|
|
|
|
GlobalVarName = "__FUNCTION__.";
|
|
|
|
break;
|
|
|
|
case PreDefinedExpr::PrettyFunction:
|
|
|
|
// FIXME:: Demangle C++ method names
|
|
|
|
GlobalVarName = "__PRETTY_FUNCTION__.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalVarName += CurFuncDecl->getName();
|
|
|
|
|
|
|
|
// FIXME: Can cache/reuse these within the module.
|
|
|
|
llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
|
|
|
|
|
|
|
|
// Create a global variable for this.
|
|
|
|
C = new llvm::GlobalVariable(C->getType(), true,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
|
|
|
C, GlobalVarName, CurFn->getParent());
|
|
|
|
llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
|
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
|
|
|
C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
|
|
|
|
return LValue::MakeAddr(C);
|
|
|
|
}
|
|
|
|
|
2007-06-09 07:31:14 +08:00
|
|
|
LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
|
2007-07-11 05:17:59 +08:00
|
|
|
// The index must always be a pointer or integer, neither of which is an
|
|
|
|
// aggregate. Emit it.
|
2007-08-09 01:43:05 +08:00
|
|
|
llvm::Value *Idx = EmitExpr(E->getIdx()).getVal();
|
2007-06-09 07:31:14 +08:00
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
// If the base is a vector type, then we are forming a vector element lvalue
|
|
|
|
// with this subscript.
|
|
|
|
if (E->getBase()->getType()->isVectorType()) {
|
|
|
|
// Emit the vector as an lvalue to get its address.
|
|
|
|
LValue Base = EmitLValue(E->getBase());
|
|
|
|
assert(Base.isSimple() && "Can only subscript lvalue vectors here!");
|
|
|
|
// FIXME: This should properly sign/zero/extend or truncate Idx to i32.
|
|
|
|
return LValue::MakeVectorElt(Base.getAddress(), Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the base must be a pointer or integer, neither of which are
|
|
|
|
// aggregates. Emit it.
|
2007-08-09 01:43:05 +08:00
|
|
|
llvm::Value *Base = EmitExpr(E->getBase()).getVal();
|
2007-07-11 05:17:59 +08:00
|
|
|
|
2007-06-09 07:31:14 +08:00
|
|
|
// Usually the base is the pointer type, but sometimes it is the index.
|
|
|
|
// Canonicalize to have the pointer as the base.
|
2007-08-09 01:43:05 +08:00
|
|
|
QualType BaseTy = E->getBase()->getType();
|
|
|
|
QualType IdxTy = E->getIdx()->getType();
|
2007-06-09 07:31:14 +08:00
|
|
|
if (isa<llvm::PointerType>(Idx->getType())) {
|
|
|
|
std::swap(Base, Idx);
|
|
|
|
std::swap(BaseTy, IdxTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The pointer is now the base. Extend or truncate the index type to 32 or
|
|
|
|
// 64-bits.
|
|
|
|
bool IdxSigned = IdxTy->isSignedIntegerType();
|
2007-06-16 07:05:46 +08:00
|
|
|
unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
|
2007-06-09 07:31:14 +08:00
|
|
|
if (IdxBitwidth != LLVMPointerWidth)
|
2007-06-16 07:05:46 +08:00
|
|
|
Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
|
2007-06-09 07:31:14 +08:00
|
|
|
IdxSigned, "idxprom");
|
|
|
|
|
|
|
|
// We know that the pointer points to a type of the correct size, unless the
|
|
|
|
// size is a VLA.
|
2007-07-16 07:26:56 +08:00
|
|
|
if (!E->getType()->isConstantSizeType(getContext()))
|
2007-06-09 07:31:14 +08:00
|
|
|
assert(0 && "VLA idx not implemented");
|
2007-07-11 05:17:59 +08:00
|
|
|
return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
|
2007-06-09 07:31:14 +08:00
|
|
|
}
|
|
|
|
|
2007-08-03 07:37:31 +08:00
|
|
|
LValue CodeGenFunction::
|
2007-08-04 01:31:20 +08:00
|
|
|
EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
|
2007-08-03 07:37:31 +08:00
|
|
|
// Emit the base vector as an l-value.
|
|
|
|
LValue Base = EmitLValue(E->getBase());
|
|
|
|
assert(Base.isSimple() && "Can only subscript lvalue vectors here!");
|
|
|
|
|
2007-08-04 01:31:20 +08:00
|
|
|
return LValue::MakeOCUVectorElt(Base.getAddress(),
|
|
|
|
E->getEncodedElementAccess());
|
2007-08-03 07:37:31 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 02:02:12 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Expression Emission
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
RValue CodeGenFunction::EmitExpr(const Expr *E) {
|
2007-06-02 02:02:12 +08:00
|
|
|
assert(E && "Null expression?");
|
|
|
|
|
|
|
|
switch (E->getStmtClass()) {
|
|
|
|
default:
|
2007-06-21 02:30:55 +08:00
|
|
|
fprintf(stderr, "Unimplemented expr!\n");
|
2007-06-02 02:02:12 +08:00
|
|
|
E->dump();
|
2007-06-16 07:05:46 +08:00
|
|
|
return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
|
2007-06-02 13:24:33 +08:00
|
|
|
|
|
|
|
// l-values.
|
2007-06-06 04:53:16 +08:00
|
|
|
case Expr::DeclRefExprClass:
|
2007-06-11 11:52:52 +08:00
|
|
|
// DeclRef's of EnumConstantDecl's are simple rvalues.
|
|
|
|
if (const EnumConstantDecl *EC =
|
|
|
|
dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
|
2007-06-16 07:05:46 +08:00
|
|
|
return RValue::get(llvm::ConstantInt::get(EC->getInitVal()));
|
2007-06-06 04:53:16 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
2007-07-11 05:58:36 +08:00
|
|
|
case Expr::ArraySubscriptExprClass:
|
|
|
|
return EmitArraySubscriptExprRV(cast<ArraySubscriptExpr>(E));
|
2007-08-04 01:31:20 +08:00
|
|
|
case Expr::OCUVectorElementExprClass:
|
implement lvalue to rvalue conversion for ocuvector components. We can now compile stuff
like this:
typedef __attribute__(( ocu_vector_type(4) )) float float4;
float4 test1(float4 V) {
return V.wzyx+V;
}
to:
_test1:
pshufd $27, %xmm0, %xmm1
addps %xmm0, %xmm1
movaps %xmm1, %xmm0
ret
and:
_test1:
mfspr r2, 256
oris r3, r2, 4096
mtspr 256, r3
li r3, lo16(LCPI1_0)
lis r4, ha16(LCPI1_0)
lvx v3, r4, r3
vperm v3, v2, v2, v3
vaddfp v2, v3, v2
mtspr 256, r2
blr
llvm-svn: 40771
2007-08-03 08:16:29 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
2007-07-21 13:21:51 +08:00
|
|
|
case Expr::PreDefinedExprClass:
|
2007-06-06 12:54:52 +08:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return RValue::get(EmitLValue(E).getAddress());
|
2007-06-02 13:24:33 +08:00
|
|
|
|
|
|
|
// Leaf expressions.
|
|
|
|
case Expr::IntegerLiteralClass:
|
2007-06-02 02:02:12 +08:00
|
|
|
return EmitIntegerLiteral(cast<IntegerLiteral>(E));
|
2007-07-10 07:03:16 +08:00
|
|
|
case Expr::FloatingLiteralClass:
|
|
|
|
return EmitFloatingLiteral(cast<FloatingLiteral>(E));
|
2007-07-13 13:18:11 +08:00
|
|
|
case Expr::CharacterLiteralClass:
|
|
|
|
return EmitCharacterLiteral(cast<CharacterLiteral>(E));
|
2007-08-04 01:51:03 +08:00
|
|
|
case Expr::TypesCompatibleExprClass:
|
|
|
|
return EmitTypesCompatibleExpr(cast<TypesCompatibleExpr>(E));
|
2007-06-02 08:16:28 +08:00
|
|
|
|
2007-06-02 13:24:33 +08:00
|
|
|
// Operators.
|
|
|
|
case Expr::ParenExprClass:
|
|
|
|
return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
|
2007-06-03 03:33:17 +08:00
|
|
|
case Expr::UnaryOperatorClass:
|
|
|
|
return EmitUnaryOperator(cast<UnaryOperator>(E));
|
2007-07-19 02:12:07 +08:00
|
|
|
case Expr::SizeOfAlignOfTypeExprClass:
|
|
|
|
return EmitSizeAlignOf(cast<SizeOfAlignOfTypeExpr>(E)->getArgumentType(),
|
|
|
|
E->getType(),
|
|
|
|
cast<SizeOfAlignOfTypeExpr>(E)->isSizeOf());
|
2007-07-14 04:25:53 +08:00
|
|
|
case Expr::ImplicitCastExprClass:
|
|
|
|
return EmitCastExpr(cast<ImplicitCastExpr>(E)->getSubExpr(), E->getType());
|
2007-06-06 04:53:16 +08:00
|
|
|
case Expr::CastExprClass:
|
2007-07-14 04:25:53 +08:00
|
|
|
return EmitCastExpr(cast<CastExpr>(E)->getSubExpr(), E->getType());
|
2007-06-16 05:34:29 +08:00
|
|
|
case Expr::CallExprClass:
|
|
|
|
return EmitCallExpr(cast<CallExpr>(E));
|
2007-06-02 13:24:33 +08:00
|
|
|
case Expr::BinaryOperatorClass:
|
2007-06-02 08:16:28 +08:00
|
|
|
return EmitBinaryOperator(cast<BinaryOperator>(E));
|
2007-07-13 13:18:11 +08:00
|
|
|
|
|
|
|
case Expr::ConditionalOperatorClass:
|
|
|
|
return EmitConditionalOperator(cast<ConditionalOperator>(E));
|
2007-08-04 08:20:15 +08:00
|
|
|
case Expr::ChooseExprClass:
|
|
|
|
return EmitChooseExpr(cast<ChooseExpr>(E));
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
RValue CodeGenFunction::EmitIntegerLiteral(const IntegerLiteral *E) {
|
2007-06-16 07:05:46 +08:00
|
|
|
return RValue::get(llvm::ConstantInt::get(E->getValue()));
|
2007-06-02 02:02:12 +08:00
|
|
|
}
|
2007-07-10 07:03:16 +08:00
|
|
|
RValue CodeGenFunction::EmitFloatingLiteral(const FloatingLiteral *E) {
|
|
|
|
return RValue::get(llvm::ConstantFP::get(ConvertType(E->getType()),
|
|
|
|
E->getValue()));
|
|
|
|
}
|
2007-07-13 13:18:11 +08:00
|
|
|
RValue CodeGenFunction::EmitCharacterLiteral(const CharacterLiteral *E) {
|
|
|
|
return RValue::get(llvm::ConstantInt::get(ConvertType(E->getType()),
|
|
|
|
E->getValue()));
|
|
|
|
}
|
2007-07-11 05:58:36 +08:00
|
|
|
|
2007-08-04 01:51:03 +08:00
|
|
|
RValue CodeGenFunction::EmitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
|
|
|
|
return RValue::get(llvm::ConstantInt::get(ConvertType(E->getType()),
|
|
|
|
E->typesAreCompatible()));
|
|
|
|
}
|
|
|
|
|
2007-08-04 08:20:15 +08:00
|
|
|
/// EmitChooseExpr - Implement __builtin_choose_expr.
|
|
|
|
RValue CodeGenFunction::EmitChooseExpr(const ChooseExpr *E) {
|
|
|
|
llvm::APSInt CondVal(32);
|
|
|
|
bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, getContext());
|
|
|
|
assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
|
|
|
|
|
|
|
|
// Emit the LHS or RHS as appropriate.
|
|
|
|
return EmitExpr(CondVal != 0 ? E->getLHS() : E->getRHS());
|
|
|
|
}
|
|
|
|
|
2007-08-04 01:51:03 +08:00
|
|
|
|
2007-07-11 05:58:36 +08:00
|
|
|
RValue CodeGenFunction::EmitArraySubscriptExprRV(const ArraySubscriptExpr *E) {
|
|
|
|
// Emit subscript expressions in rvalue context's. For most cases, this just
|
|
|
|
// loads the lvalue formed by the subscript expr. However, we have to be
|
|
|
|
// careful, because the base of a vector subscript is occasionally an rvalue,
|
|
|
|
// so we can't get it as an lvalue.
|
|
|
|
if (!E->getBase()->getType()->isVectorType())
|
|
|
|
return EmitLoadOfLValue(E);
|
|
|
|
|
|
|
|
// Handle the vector case. The base must be a vector, the index must be an
|
|
|
|
// integer value.
|
2007-08-09 01:43:05 +08:00
|
|
|
llvm::Value *Base = EmitExpr(E->getBase()).getVal();
|
|
|
|
llvm::Value *Idx = EmitExpr(E->getIdx()).getVal();
|
2007-07-11 05:58:36 +08:00
|
|
|
|
|
|
|
// FIXME: Convert Idx to i32 type.
|
|
|
|
|
|
|
|
return RValue::get(Builder.CreateExtractElement(Base, Idx, "vecext"));
|
|
|
|
}
|
|
|
|
|
2007-07-14 04:25:53 +08:00
|
|
|
// EmitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
|
|
|
|
// have to handle a more broad range of conversions than explicit casts, as they
|
|
|
|
// handle things like function to ptr-to-function decay etc.
|
|
|
|
RValue CodeGenFunction::EmitCastExpr(const Expr *Op, QualType DestTy) {
|
2007-08-09 01:43:05 +08:00
|
|
|
RValue Src = EmitExpr(Op);
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
// If the destination is void, just evaluate the source.
|
2007-07-14 04:25:53 +08:00
|
|
|
if (DestTy->isVoidType())
|
2007-06-06 04:53:16 +08:00
|
|
|
return RValue::getAggregate(0);
|
|
|
|
|
2007-08-09 01:43:05 +08:00
|
|
|
return EmitConversion(Src, Op->getType(), DestTy);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-06-16 05:34:29 +08:00
|
|
|
RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
|
2007-08-09 01:43:05 +08:00
|
|
|
llvm::Value *Callee = EmitExpr(E->getCallee()).getVal();
|
2007-07-11 06:18:37 +08:00
|
|
|
|
|
|
|
// The callee type will always be a pointer to function type, get the function
|
|
|
|
// type.
|
2007-08-09 01:43:05 +08:00
|
|
|
QualType CalleeTy = E->getCallee()->getType();
|
2007-07-11 06:18:37 +08:00
|
|
|
CalleeTy = cast<PointerType>(CalleeTy.getCanonicalType())->getPointeeType();
|
|
|
|
|
|
|
|
// Get information about the argument types.
|
|
|
|
FunctionTypeProto::arg_type_iterator ArgTyIt = 0, ArgTyEnd = 0;
|
|
|
|
|
|
|
|
// Calling unprototyped functions provides no argument info.
|
|
|
|
if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(CalleeTy)) {
|
|
|
|
ArgTyIt = FTP->arg_type_begin();
|
|
|
|
ArgTyEnd = FTP->arg_type_end();
|
|
|
|
}
|
2007-06-16 05:34:29 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::SmallVector<llvm::Value*, 16> Args;
|
2007-06-16 05:34:29 +08:00
|
|
|
|
|
|
|
// FIXME: Handle struct return.
|
|
|
|
for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
|
2007-08-09 01:43:05 +08:00
|
|
|
QualType ArgTy = E->getArg(i)->getType();
|
|
|
|
RValue ArgVal = EmitExpr(E->getArg(i));
|
2007-07-11 06:18:37 +08:00
|
|
|
|
|
|
|
// If this argument has prototype information, convert it.
|
|
|
|
if (ArgTyIt != ArgTyEnd) {
|
|
|
|
ArgVal = EmitConversion(ArgVal, ArgTy, *ArgTyIt++);
|
|
|
|
} else {
|
|
|
|
// Otherwise, if passing through "..." or to a function with no prototype,
|
|
|
|
// perform the "default argument promotions" (C99 6.5.2.2p6), which
|
|
|
|
// includes the usual unary conversions, but also promotes float to
|
|
|
|
// double.
|
|
|
|
if (const BuiltinType *BT =
|
|
|
|
dyn_cast<BuiltinType>(ArgTy.getCanonicalType())) {
|
|
|
|
if (BT->getKind() == BuiltinType::Float)
|
|
|
|
ArgVal = RValue::get(Builder.CreateFPExt(ArgVal.getVal(),
|
|
|
|
llvm::Type::DoubleTy,"tmp"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-16 05:34:29 +08:00
|
|
|
|
|
|
|
if (ArgVal.isScalar())
|
|
|
|
Args.push_back(ArgVal.getVal());
|
|
|
|
else // Pass by-address. FIXME: Set attribute bit on call.
|
2007-06-23 02:48:09 +08:00
|
|
|
Args.push_back(ArgVal.getAggregateAddr());
|
2007-06-16 05:34:29 +08:00
|
|
|
}
|
|
|
|
|
2007-08-01 14:24:52 +08:00
|
|
|
llvm::Value *V = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
|
2007-06-16 05:34:29 +08:00
|
|
|
if (V->getType() != llvm::Type::VoidTy)
|
|
|
|
V->setName("call");
|
|
|
|
|
|
|
|
// FIXME: Struct return;
|
|
|
|
return RValue::get(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Unary Operator Emission
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-03 03:33:17 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
RValue CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
|
2007-06-03 03:33:17 +08:00
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default:
|
|
|
|
printf("Unimplemented unary expr!\n");
|
|
|
|
E->dump();
|
2007-06-16 07:05:46 +08:00
|
|
|
return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
|
2007-07-12 07:43:46 +08:00
|
|
|
case UnaryOperator::PostInc:
|
|
|
|
case UnaryOperator::PostDec:
|
|
|
|
case UnaryOperator::PreInc :
|
|
|
|
case UnaryOperator::PreDec : return EmitUnaryIncDec(E);
|
|
|
|
case UnaryOperator::AddrOf : return EmitUnaryAddrOf(E);
|
|
|
|
case UnaryOperator::Deref : return EmitLoadOfLValue(E);
|
|
|
|
case UnaryOperator::Plus : return EmitUnaryPlus(E);
|
|
|
|
case UnaryOperator::Minus : return EmitUnaryMinus(E);
|
|
|
|
case UnaryOperator::Not : return EmitUnaryNot(E);
|
|
|
|
case UnaryOperator::LNot : return EmitUnaryLNot(E);
|
2007-07-19 02:12:07 +08:00
|
|
|
case UnaryOperator::SizeOf :
|
|
|
|
return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
|
|
|
|
case UnaryOperator::AlignOf :
|
|
|
|
return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
|
2007-06-06 04:53:16 +08:00
|
|
|
// FIXME: real/imag
|
|
|
|
case UnaryOperator::Extension: return EmitExpr(E->getSubExpr());
|
2007-06-03 03:33:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-12 07:43:46 +08:00
|
|
|
RValue CodeGenFunction::EmitUnaryIncDec(const UnaryOperator *E) {
|
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
RValue InVal = EmitLoadOfLValue(LV, E->getSubExpr()->getType());
|
|
|
|
|
|
|
|
// We know the operand is real or pointer type, so it must be an LLVM scalar.
|
|
|
|
assert(InVal.isScalar() && "Unknown thing to increment");
|
|
|
|
llvm::Value *InV = InVal.getVal();
|
|
|
|
|
|
|
|
int AmountVal = 1;
|
|
|
|
if (E->getOpcode() == UnaryOperator::PreDec ||
|
|
|
|
E->getOpcode() == UnaryOperator::PostDec)
|
|
|
|
AmountVal = -1;
|
|
|
|
|
|
|
|
llvm::Value *NextVal;
|
|
|
|
if (isa<llvm::IntegerType>(InV->getType())) {
|
|
|
|
NextVal = llvm::ConstantInt::get(InV->getType(), AmountVal);
|
|
|
|
NextVal = Builder.CreateAdd(InV, NextVal, AmountVal == 1 ? "inc" : "dec");
|
|
|
|
} else if (InV->getType()->isFloatingPoint()) {
|
|
|
|
NextVal = llvm::ConstantFP::get(InV->getType(), AmountVal);
|
|
|
|
NextVal = Builder.CreateAdd(InV, NextVal, AmountVal == 1 ? "inc" : "dec");
|
|
|
|
} else {
|
|
|
|
// FIXME: This is not right for pointers to VLA types.
|
|
|
|
assert(isa<llvm::PointerType>(InV->getType()));
|
|
|
|
NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
|
|
|
|
NextVal = Builder.CreateGEP(InV, NextVal, AmountVal == 1 ? "inc" : "dec");
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue NextValToStore = RValue::get(NextVal);
|
|
|
|
|
|
|
|
// Store the updated result through the lvalue.
|
|
|
|
EmitStoreThroughLValue(NextValToStore, LV, E->getSubExpr()->getType());
|
|
|
|
|
|
|
|
// If this is a postinc, return the value read from memory, otherwise use the
|
|
|
|
// updated value.
|
|
|
|
if (E->getOpcode() == UnaryOperator::PreDec ||
|
|
|
|
E->getOpcode() == UnaryOperator::PreInc)
|
|
|
|
return NextValToStore;
|
|
|
|
else
|
|
|
|
return InVal;
|
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// C99 6.5.3.2
|
|
|
|
RValue CodeGenFunction::EmitUnaryAddrOf(const UnaryOperator *E) {
|
|
|
|
// The address of the operand is just its lvalue. It cannot be a bitfield.
|
|
|
|
return RValue::get(EmitLValue(E->getSubExpr()).getAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitUnaryPlus(const UnaryOperator *E) {
|
2007-08-09 01:43:05 +08:00
|
|
|
assert(E->getType().getCanonicalType() ==
|
|
|
|
E->getSubExpr()->getType().getCanonicalType() && "Bad unary plus!");
|
|
|
|
// Unary plus just returns its value.
|
|
|
|
return EmitExpr(E->getSubExpr());
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitUnaryMinus(const UnaryOperator *E) {
|
2007-08-09 01:43:05 +08:00
|
|
|
assert(E->getType().getCanonicalType() ==
|
|
|
|
E->getSubExpr()->getType().getCanonicalType() && "Bad unary minus!");
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
// Unary minus performs promotions, then negates its arithmetic operand.
|
2007-08-09 01:43:05 +08:00
|
|
|
RValue V = EmitExpr(E->getSubExpr());
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
if (V.isScalar())
|
|
|
|
return RValue::get(Builder.CreateNeg(V.getVal(), "neg"));
|
2007-06-03 03:33:17 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
assert(0 && "FIXME: This doesn't handle complex operands yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitUnaryNot(const UnaryOperator *E) {
|
|
|
|
// Unary not performs promotions, then complements its integer operand.
|
2007-08-09 01:43:05 +08:00
|
|
|
RValue V = EmitExpr(E->getSubExpr());
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
if (V.isScalar())
|
|
|
|
return RValue::get(Builder.CreateNot(V.getVal(), "neg"));
|
|
|
|
|
|
|
|
assert(0 && "FIXME: This doesn't handle integer complex operands yet (GNU)");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// C99 6.5.3.3
|
|
|
|
RValue CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
|
|
|
|
// Compare operand to zero.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *BoolVal = EvaluateExprAsBool(E->getSubExpr());
|
2007-06-03 03:33:17 +08:00
|
|
|
|
|
|
|
// Invert value.
|
2007-06-03 03:47:04 +08:00
|
|
|
// TODO: Could dynamically modify easy computations here. For example, if
|
|
|
|
// the operand is an icmp ne, turn into icmp eq.
|
2007-06-03 03:33:17 +08:00
|
|
|
BoolVal = Builder.CreateNot(BoolVal, "lnot");
|
|
|
|
|
|
|
|
// ZExt result to int.
|
2007-06-06 04:53:16 +08:00
|
|
|
return RValue::get(Builder.CreateZExt(BoolVal, LLVMIntTy, "lnot.ext"));
|
2007-06-03 03:33:17 +08:00
|
|
|
}
|
|
|
|
|
2007-07-19 02:12:07 +08:00
|
|
|
/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
|
|
|
|
/// an integer (RetType).
|
|
|
|
RValue CodeGenFunction::EmitSizeAlignOf(QualType TypeToSize,
|
|
|
|
QualType RetType, bool isSizeOf) {
|
|
|
|
/// FIXME: This doesn't handle VLAs yet!
|
|
|
|
std::pair<uint64_t, unsigned> Info =
|
|
|
|
getContext().getTypeInfo(TypeToSize, SourceLocation());
|
|
|
|
|
|
|
|
uint64_t Val = isSizeOf ? Info.first : Info.second;
|
|
|
|
Val /= 8; // Return size in bytes, not bits.
|
|
|
|
|
|
|
|
assert(RetType->isIntegerType() && "Result type must be an integer!");
|
|
|
|
|
|
|
|
unsigned ResultWidth = getContext().getTypeSize(RetType, SourceLocation());
|
|
|
|
return RValue::get(llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)));
|
|
|
|
}
|
|
|
|
|
2007-06-02 02:02:12 +08:00
|
|
|
|
2007-06-02 08:16:28 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Binary Operator Emission
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
2007-06-30 00:52:55 +08:00
|
|
|
/// EmitCompoundAssignmentOperands - Compound assignment operations (like +=)
|
|
|
|
/// are strange in that the result of the operation is not the same type as the
|
|
|
|
/// intermediate computation. This function emits the LHS and RHS operands of
|
|
|
|
/// the compound assignment, promoting them to their common computation type.
|
|
|
|
///
|
|
|
|
/// Since the LHS is an lvalue, and the result is stored back through it, we
|
|
|
|
/// return the lvalue as well as the LHS/RHS rvalues. On return, the LHS and
|
|
|
|
/// RHS values are both in the computation type for the operator.
|
|
|
|
void CodeGenFunction::
|
|
|
|
EmitCompoundAssignmentOperands(const CompoundAssignOperator *E,
|
|
|
|
LValue &LHSLV, RValue &LHS, RValue &RHS) {
|
|
|
|
LHSLV = EmitLValue(E->getLHS());
|
|
|
|
|
|
|
|
// Load the LHS and RHS operands.
|
|
|
|
QualType LHSTy = E->getLHS()->getType();
|
|
|
|
LHS = EmitLoadOfLValue(LHSLV, LHSTy);
|
2007-08-09 01:43:05 +08:00
|
|
|
RHS = EmitExpr(E->getRHS());
|
|
|
|
QualType RHSTy = E->getRHS()->getType();
|
2007-06-30 01:26:27 +08:00
|
|
|
|
2007-06-30 00:52:55 +08:00
|
|
|
// Convert the LHS and RHS to the common evaluation type.
|
|
|
|
LHS = EmitConversion(LHS, LHSTy, E->getComputationType());
|
|
|
|
RHS = EmitConversion(RHS, RHSTy, E->getComputationType());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitCompoundAssignmentResult - Given a result value in the computation type,
|
|
|
|
/// truncate it down to the actual result type, store it through the LHS lvalue,
|
|
|
|
/// and return it.
|
|
|
|
RValue CodeGenFunction::
|
|
|
|
EmitCompoundAssignmentResult(const CompoundAssignOperator *E,
|
|
|
|
LValue LHSLV, RValue ResV) {
|
|
|
|
|
|
|
|
// Truncate back to the destination type.
|
|
|
|
if (E->getComputationType() != E->getType())
|
|
|
|
ResV = EmitConversion(ResV, E->getComputationType(), E->getType());
|
|
|
|
|
|
|
|
// Store the result value into the LHS.
|
|
|
|
EmitStoreThroughLValue(ResV, LHSLV, E->getType());
|
|
|
|
|
|
|
|
// Return the result.
|
|
|
|
return ResV;
|
|
|
|
}
|
|
|
|
|
2007-06-02 08:16:28 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
|
2007-06-30 00:52:55 +08:00
|
|
|
RValue LHS, RHS;
|
2007-06-02 08:16:28 +08:00
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default:
|
2007-06-30 01:03:06 +08:00
|
|
|
fprintf(stderr, "Unimplemented binary expr!\n");
|
2007-06-02 08:16:28 +08:00
|
|
|
E->dump();
|
2007-06-16 07:05:46 +08:00
|
|
|
return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
|
2007-06-30 01:03:06 +08:00
|
|
|
case BinaryOperator::Mul:
|
2007-08-09 01:49:18 +08:00
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
2007-06-30 01:03:06 +08:00
|
|
|
return EmitMul(LHS, RHS, E->getType());
|
|
|
|
case BinaryOperator::Div:
|
2007-08-09 01:49:18 +08:00
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
2007-06-30 01:03:06 +08:00
|
|
|
return EmitDiv(LHS, RHS, E->getType());
|
|
|
|
case BinaryOperator::Rem:
|
2007-08-09 01:49:18 +08:00
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
2007-06-30 01:03:06 +08:00
|
|
|
return EmitRem(LHS, RHS, E->getType());
|
2007-08-09 01:49:18 +08:00
|
|
|
case BinaryOperator::Add:
|
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
|
|
|
if (!E->getType()->isPointerType())
|
|
|
|
return EmitAdd(LHS, RHS, E->getType());
|
|
|
|
|
|
|
|
return EmitPointerAdd(LHS, E->getLHS()->getType(),
|
|
|
|
RHS, E->getRHS()->getType(), E->getType());
|
|
|
|
case BinaryOperator::Sub:
|
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
|
|
|
|
|
|
|
if (!E->getLHS()->getType()->isPointerType())
|
|
|
|
return EmitSub(LHS, RHS, E->getType());
|
|
|
|
|
|
|
|
return EmitPointerSub(LHS, E->getLHS()->getType(),
|
|
|
|
RHS, E->getRHS()->getType(), E->getType());
|
2007-06-30 01:26:27 +08:00
|
|
|
case BinaryOperator::Shl:
|
2007-08-09 01:43:05 +08:00
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
2007-06-30 01:26:27 +08:00
|
|
|
return EmitShl(LHS, RHS, E->getType());
|
|
|
|
case BinaryOperator::Shr:
|
2007-08-09 01:43:05 +08:00
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
2007-06-30 01:26:27 +08:00
|
|
|
return EmitShr(LHS, RHS, E->getType());
|
2007-06-30 01:03:06 +08:00
|
|
|
case BinaryOperator::And:
|
2007-08-09 01:49:18 +08:00
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
2007-06-30 01:03:06 +08:00
|
|
|
return EmitAnd(LHS, RHS, E->getType());
|
|
|
|
case BinaryOperator::Xor:
|
2007-08-09 01:49:18 +08:00
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
2007-06-30 01:03:06 +08:00
|
|
|
return EmitXor(LHS, RHS, E->getType());
|
|
|
|
case BinaryOperator::Or :
|
2007-08-09 01:49:18 +08:00
|
|
|
LHS = EmitExpr(E->getLHS());
|
|
|
|
RHS = EmitExpr(E->getRHS());
|
2007-06-30 01:03:06 +08:00
|
|
|
return EmitOr(LHS, RHS, E->getType());
|
2007-06-06 04:53:16 +08:00
|
|
|
case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
|
|
|
|
case BinaryOperator::LOr: return EmitBinaryLOr(E);
|
2007-06-21 02:30:55 +08:00
|
|
|
case BinaryOperator::LT:
|
|
|
|
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULT,
|
|
|
|
llvm::ICmpInst::ICMP_SLT,
|
|
|
|
llvm::FCmpInst::FCMP_OLT);
|
|
|
|
case BinaryOperator::GT:
|
|
|
|
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGT,
|
|
|
|
llvm::ICmpInst::ICMP_SGT,
|
|
|
|
llvm::FCmpInst::FCMP_OGT);
|
|
|
|
case BinaryOperator::LE:
|
|
|
|
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULE,
|
|
|
|
llvm::ICmpInst::ICMP_SLE,
|
|
|
|
llvm::FCmpInst::FCMP_OLE);
|
|
|
|
case BinaryOperator::GE:
|
|
|
|
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGE,
|
|
|
|
llvm::ICmpInst::ICMP_SGE,
|
|
|
|
llvm::FCmpInst::FCMP_OGE);
|
|
|
|
case BinaryOperator::EQ:
|
|
|
|
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_EQ,
|
|
|
|
llvm::ICmpInst::ICMP_EQ,
|
|
|
|
llvm::FCmpInst::FCMP_OEQ);
|
|
|
|
case BinaryOperator::NE:
|
|
|
|
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_NE,
|
|
|
|
llvm::ICmpInst::ICMP_NE,
|
|
|
|
llvm::FCmpInst::FCMP_UNE);
|
2007-06-30 00:52:55 +08:00
|
|
|
case BinaryOperator::Assign:
|
|
|
|
return EmitBinaryAssign(E);
|
|
|
|
|
2007-06-30 01:03:06 +08:00
|
|
|
case BinaryOperator::MulAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitMul(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
|
|
|
case BinaryOperator::DivAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitDiv(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
|
|
|
case BinaryOperator::RemAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitRem(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
2007-06-30 00:52:55 +08:00
|
|
|
case BinaryOperator::AddAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitAdd(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
|
|
|
case BinaryOperator::SubAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitSub(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
2007-06-30 01:26:27 +08:00
|
|
|
case BinaryOperator::ShlAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitShl(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
|
|
|
case BinaryOperator::ShrAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitShr(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
2007-06-30 01:03:06 +08:00
|
|
|
case BinaryOperator::AndAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitAnd(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
|
|
|
case BinaryOperator::OrAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitOr(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
|
|
|
case BinaryOperator::XorAssign: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E);
|
|
|
|
LValue LHSLV;
|
|
|
|
EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS);
|
|
|
|
LHS = EmitXor(LHS, RHS, CAO->getComputationType());
|
|
|
|
return EmitCompoundAssignmentResult(CAO, LHSLV, LHS);
|
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
case BinaryOperator::Comma: return EmitBinaryComma(E);
|
2007-06-02 08:16:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:03:06 +08:00
|
|
|
RValue CodeGenFunction::EmitMul(RValue LHS, RValue RHS, QualType ResTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.isScalar())
|
|
|
|
return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul"));
|
|
|
|
|
2007-07-14 07:33:18 +08:00
|
|
|
// Otherwise, this must be a complex number.
|
|
|
|
llvm::Value *LHSR, *LHSI, *RHSR, *RHSI;
|
|
|
|
|
|
|
|
EmitLoadOfComplex(LHS, LHSR, LHSI);
|
|
|
|
EmitLoadOfComplex(RHS, RHSR, RHSI);
|
|
|
|
|
|
|
|
llvm::Value *ResRl = Builder.CreateMul(LHSR, RHSR, "mul.rl");
|
|
|
|
llvm::Value *ResRr = Builder.CreateMul(LHSI, RHSI, "mul.rr");
|
|
|
|
llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
|
|
|
|
|
|
|
|
llvm::Value *ResIl = Builder.CreateMul(LHSI, RHSR, "mul.il");
|
|
|
|
llvm::Value *ResIr = Builder.CreateMul(LHSR, RHSI, "mul.ir");
|
|
|
|
llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
|
|
|
|
|
|
|
|
llvm::Value *Res = CreateTempAlloca(ConvertType(ResTy));
|
|
|
|
EmitStoreOfComplex(ResR, ResI, Res);
|
|
|
|
return RValue::getAggregate(Res);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
2007-06-02 08:16:28 +08:00
|
|
|
|
2007-06-30 01:03:06 +08:00
|
|
|
RValue CodeGenFunction::EmitDiv(RValue LHS, RValue RHS, QualType ResTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.isScalar()) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *RV;
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.getVal()->getType()->isFloatingPoint())
|
|
|
|
RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div");
|
2007-06-30 01:03:06 +08:00
|
|
|
else if (ResTy->isUnsignedIntegerType())
|
2007-06-06 04:53:16 +08:00
|
|
|
RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div");
|
|
|
|
else
|
|
|
|
RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div");
|
|
|
|
return RValue::get(RV);
|
|
|
|
}
|
|
|
|
assert(0 && "FIXME: This doesn't handle complex operands yet");
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:03:06 +08:00
|
|
|
RValue CodeGenFunction::EmitRem(RValue LHS, RValue RHS, QualType ResTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.isScalar()) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *RV;
|
2007-06-06 04:53:16 +08:00
|
|
|
// Rem in C can't be a floating point type: C99 6.5.5p2.
|
2007-06-30 01:03:06 +08:00
|
|
|
if (ResTy->isUnsignedIntegerType())
|
2007-06-06 04:53:16 +08:00
|
|
|
RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem");
|
|
|
|
else
|
|
|
|
RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem");
|
|
|
|
return RValue::get(RV);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0 && "FIXME: This doesn't handle complex operands yet");
|
|
|
|
}
|
2007-06-02 08:16:28 +08:00
|
|
|
|
2007-06-30 00:52:55 +08:00
|
|
|
RValue CodeGenFunction::EmitAdd(RValue LHS, RValue RHS, QualType ResTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.isScalar())
|
|
|
|
return RValue::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "add"));
|
2007-06-30 00:52:55 +08:00
|
|
|
|
2007-06-23 05:44:33 +08:00
|
|
|
// Otherwise, this must be a complex number.
|
|
|
|
llvm::Value *LHSR, *LHSI, *RHSR, *RHSI;
|
|
|
|
|
|
|
|
EmitLoadOfComplex(LHS, LHSR, LHSI);
|
|
|
|
EmitLoadOfComplex(RHS, RHSR, RHSI);
|
|
|
|
|
|
|
|
llvm::Value *ResR = Builder.CreateAdd(LHSR, RHSR, "add.r");
|
|
|
|
llvm::Value *ResI = Builder.CreateAdd(LHSI, RHSI, "add.i");
|
|
|
|
|
2007-06-30 00:52:55 +08:00
|
|
|
llvm::Value *Res = CreateTempAlloca(ConvertType(ResTy));
|
2007-06-23 05:44:33 +08:00
|
|
|
EmitStoreOfComplex(ResR, ResI, Res);
|
|
|
|
return RValue::getAggregate(Res);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-07-13 11:05:23 +08:00
|
|
|
RValue CodeGenFunction::EmitPointerAdd(RValue LHS, QualType LHSTy,
|
|
|
|
RValue RHS, QualType RHSTy,
|
|
|
|
QualType ResTy) {
|
|
|
|
llvm::Value *LHSValue = LHS.getVal();
|
|
|
|
llvm::Value *RHSValue = RHS.getVal();
|
|
|
|
if (LHSTy->isPointerType()) {
|
|
|
|
// pointer + int
|
|
|
|
return RValue::get(Builder.CreateGEP(LHSValue, RHSValue, "add.ptr"));
|
|
|
|
} else {
|
|
|
|
// int + pointer
|
|
|
|
return RValue::get(Builder.CreateGEP(RHSValue, LHSValue, "add.ptr"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-30 00:52:55 +08:00
|
|
|
RValue CodeGenFunction::EmitSub(RValue LHS, RValue RHS, QualType ResTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.isScalar())
|
|
|
|
return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub"));
|
|
|
|
|
|
|
|
assert(0 && "FIXME: This doesn't handle complex operands yet");
|
|
|
|
}
|
|
|
|
|
2007-07-13 11:05:23 +08:00
|
|
|
RValue CodeGenFunction::EmitPointerSub(RValue LHS, QualType LHSTy,
|
|
|
|
RValue RHS, QualType RHSTy,
|
|
|
|
QualType ResTy) {
|
|
|
|
llvm::Value *LHSValue = LHS.getVal();
|
|
|
|
llvm::Value *RHSValue = RHS.getVal();
|
|
|
|
if (const PointerType *RHSPtrType =
|
|
|
|
dyn_cast<PointerType>(RHSTy.getTypePtr())) {
|
|
|
|
// pointer - pointer
|
|
|
|
const PointerType *LHSPtrType = cast<PointerType>(LHSTy.getTypePtr());
|
|
|
|
QualType LHSElementType = LHSPtrType->getPointeeType();
|
|
|
|
assert(LHSElementType == RHSPtrType->getPointeeType() &&
|
|
|
|
"can't subtract pointers with differing element types");
|
Add a hack (mirroring llvm-gcc) to pointer difference
codegen to compile:
int test(int *A, int *B) {
return A-B;
}
into:
_test:
movl 4(%esp), %eax
subl 8(%esp), %eax
sarl $2, %eax
ret
instead of:
_test:
movl 4(%esp), %eax
subl 8(%esp), %eax
movl %eax, %ecx
sarl $31, %ecx
shrl $30, %ecx
addl %ecx, %eax
sarl $2, %eax
ret
llvm-svn: 39902
2007-07-16 13:43:05 +08:00
|
|
|
uint64_t ElementSize = getContext().getTypeSize(LHSElementType,
|
2007-07-14 09:29:45 +08:00
|
|
|
SourceLocation()) / 8;
|
2007-07-13 11:05:23 +08:00
|
|
|
const llvm::Type *ResultType = ConvertType(ResTy);
|
|
|
|
llvm::Value *CastLHS = Builder.CreatePtrToInt(LHSValue, ResultType,
|
|
|
|
"sub.ptr.lhs.cast");
|
|
|
|
llvm::Value *CastRHS = Builder.CreatePtrToInt(RHSValue, ResultType,
|
|
|
|
"sub.ptr.rhs.cast");
|
|
|
|
llvm::Value *BytesBetween = Builder.CreateSub(CastLHS, CastRHS,
|
|
|
|
"sub.ptr.sub");
|
Add a hack (mirroring llvm-gcc) to pointer difference
codegen to compile:
int test(int *A, int *B) {
return A-B;
}
into:
_test:
movl 4(%esp), %eax
subl 8(%esp), %eax
sarl $2, %eax
ret
instead of:
_test:
movl 4(%esp), %eax
subl 8(%esp), %eax
movl %eax, %ecx
sarl $31, %ecx
shrl $30, %ecx
addl %ecx, %eax
sarl $2, %eax
ret
llvm-svn: 39902
2007-07-16 13:43:05 +08:00
|
|
|
|
|
|
|
// HACK: LLVM doesn't have an divide instruction that 'knows' there is no
|
|
|
|
// remainder. As such, we handle common power-of-two cases here to generate
|
|
|
|
// better code.
|
|
|
|
if (llvm::isPowerOf2_64(ElementSize)) {
|
|
|
|
llvm::Value *ShAmt =
|
|
|
|
llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
|
|
|
|
return RValue::get(Builder.CreateAShr(BytesBetween, ShAmt,"sub.ptr.shr"));
|
|
|
|
} else {
|
|
|
|
// Otherwise, do a full sdiv.
|
|
|
|
llvm::Value *BytesPerElement =
|
|
|
|
llvm::ConstantInt::get(ResultType, ElementSize);
|
|
|
|
return RValue::get(Builder.CreateSDiv(BytesBetween, BytesPerElement,
|
|
|
|
"sub.ptr.div"));
|
|
|
|
}
|
2007-07-13 11:05:23 +08:00
|
|
|
} else {
|
|
|
|
// pointer - int
|
|
|
|
llvm::Value *NegatedRHS = Builder.CreateNeg(RHSValue, "sub.ptr.neg");
|
|
|
|
return RValue::get(Builder.CreateGEP(LHSValue, NegatedRHS, "sub.ptr"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:26:27 +08:00
|
|
|
RValue CodeGenFunction::EmitShl(RValue LHSV, RValue RHSV, QualType ResTy) {
|
|
|
|
llvm::Value *LHS = LHSV.getVal(), *RHS = RHSV.getVal();
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
// LLVM requires the LHS and RHS to be the same type, promote or truncate the
|
|
|
|
// RHS to the same size as the LHS.
|
|
|
|
if (LHS->getType() != RHS->getType())
|
|
|
|
RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
|
|
|
|
|
|
|
|
return RValue::get(Builder.CreateShl(LHS, RHS, "shl"));
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:26:27 +08:00
|
|
|
RValue CodeGenFunction::EmitShr(RValue LHSV, RValue RHSV, QualType ResTy) {
|
|
|
|
llvm::Value *LHS = LHSV.getVal(), *RHS = RHSV.getVal();
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
// LLVM requires the LHS and RHS to be the same type, promote or truncate the
|
|
|
|
// RHS to the same size as the LHS.
|
|
|
|
if (LHS->getType() != RHS->getType())
|
|
|
|
RHS = Builder.CreateIntCast(RHS, LHS->getType(), false, "sh_prom");
|
|
|
|
|
2007-06-30 01:26:27 +08:00
|
|
|
if (ResTy->isUnsignedIntegerType())
|
2007-06-06 04:53:16 +08:00
|
|
|
return RValue::get(Builder.CreateLShr(LHS, RHS, "shr"));
|
|
|
|
else
|
|
|
|
return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
|
|
|
|
}
|
|
|
|
|
2007-06-21 02:30:55 +08:00
|
|
|
RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E,
|
|
|
|
unsigned UICmpOpc, unsigned SICmpOpc,
|
|
|
|
unsigned FCmpOpc) {
|
2007-08-09 01:49:18 +08:00
|
|
|
RValue LHS = EmitExpr(E->getLHS());
|
|
|
|
RValue RHS = EmitExpr(E->getRHS());
|
2007-06-21 02:02:30 +08:00
|
|
|
|
|
|
|
llvm::Value *Result;
|
|
|
|
if (LHS.isScalar()) {
|
|
|
|
if (LHS.getVal()->getType()->isFloatingPoint()) {
|
2007-06-21 02:30:55 +08:00
|
|
|
Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
|
|
|
|
LHS.getVal(), RHS.getVal(), "cmp");
|
|
|
|
} else if (E->getLHS()->getType()->isUnsignedIntegerType()) {
|
|
|
|
// FIXME: This check isn't right for "unsigned short < int" where ushort
|
|
|
|
// promotes to int and does a signed compare.
|
|
|
|
Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
|
|
|
|
LHS.getVal(), RHS.getVal(), "cmp");
|
2007-06-21 02:02:30 +08:00
|
|
|
} else {
|
2007-06-21 02:30:55 +08:00
|
|
|
// Signed integers and pointers.
|
|
|
|
Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
|
|
|
|
LHS.getVal(), RHS.getVal(), "cmp");
|
2007-06-21 02:02:30 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Struct/union/complex
|
2007-07-14 07:33:18 +08:00
|
|
|
llvm::Value *LHSR, *LHSI, *RHSR, *RHSI, *ResultR, *ResultI;
|
|
|
|
EmitLoadOfComplex(LHS, LHSR, LHSI);
|
|
|
|
EmitLoadOfComplex(RHS, RHSR, RHSI);
|
|
|
|
|
2007-07-15 04:05:18 +08:00
|
|
|
// FIXME: need to consider _Complex over integers too!
|
|
|
|
|
2007-07-14 07:33:18 +08:00
|
|
|
ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
|
|
|
|
LHSR, RHSR, "cmp.r");
|
|
|
|
ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
|
|
|
|
LHSI, RHSI, "cmp.i");
|
|
|
|
if (BinaryOperator::EQ == E->getOpcode()) {
|
|
|
|
Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
|
|
|
|
} else if (BinaryOperator::NE == E->getOpcode()) {
|
|
|
|
Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
|
|
|
|
} else {
|
|
|
|
assert(0 && "Complex comparison other than == or != ?");
|
|
|
|
}
|
2007-06-21 02:02:30 +08:00
|
|
|
}
|
2007-07-14 07:33:18 +08:00
|
|
|
|
2007-06-21 02:02:30 +08:00
|
|
|
// ZExt result to int.
|
|
|
|
return RValue::get(Builder.CreateZExt(Result, LLVMIntTy, "cmp.ext"));
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:03:06 +08:00
|
|
|
RValue CodeGenFunction::EmitAnd(RValue LHS, RValue RHS, QualType ResTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.isScalar())
|
|
|
|
return RValue::get(Builder.CreateAnd(LHS.getVal(), RHS.getVal(), "and"));
|
|
|
|
|
|
|
|
assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:03:06 +08:00
|
|
|
RValue CodeGenFunction::EmitXor(RValue LHS, RValue RHS, QualType ResTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.isScalar())
|
|
|
|
return RValue::get(Builder.CreateXor(LHS.getVal(), RHS.getVal(), "xor"));
|
|
|
|
|
|
|
|
assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
|
|
|
|
}
|
|
|
|
|
2007-06-30 01:03:06 +08:00
|
|
|
RValue CodeGenFunction::EmitOr(RValue LHS, RValue RHS, QualType ResTy) {
|
2007-06-06 04:53:16 +08:00
|
|
|
if (LHS.isScalar())
|
|
|
|
return RValue::get(Builder.CreateOr(LHS.getVal(), RHS.getVal(), "or"));
|
|
|
|
|
|
|
|
assert(0 && "FIXME: This doesn't handle complex integer operands yet (GNU)");
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitBinaryLAnd(const BinaryOperator *E) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
|
|
|
|
llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
|
2007-06-06 04:53:16 +08:00
|
|
|
Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
|
|
|
|
|
|
|
|
EmitBlock(RHSBlock);
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
// Reaquire the RHS block, as there may be subblocks inserted.
|
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
|
|
|
EmitBlock(ContBlock);
|
|
|
|
|
|
|
|
// Create a PHI node. If we just evaluted the LHS condition, the result is
|
|
|
|
// false. If we evaluated both, the result is the RHS condition.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
|
2007-06-06 04:53:16 +08:00
|
|
|
PN->reserveOperandSpace(2);
|
2007-06-16 07:05:46 +08:00
|
|
|
PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
|
2007-06-06 04:53:16 +08:00
|
|
|
PN->addIncoming(RHSCond, RHSBlock);
|
|
|
|
|
|
|
|
// ZExt result to int.
|
|
|
|
return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "land.ext"));
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitBinaryLOr(const BinaryOperator *E) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *LHSCond = EvaluateExprAsBool(E->getLHS());
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
|
|
|
|
llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
|
2007-06-06 04:53:16 +08:00
|
|
|
Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
|
|
|
|
|
|
|
|
EmitBlock(RHSBlock);
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *RHSCond = EvaluateExprAsBool(E->getRHS());
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
// Reaquire the RHS block, as there may be subblocks inserted.
|
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
|
|
|
EmitBlock(ContBlock);
|
|
|
|
|
|
|
|
// Create a PHI node. If we just evaluted the LHS condition, the result is
|
|
|
|
// true. If we evaluated both, the result is the RHS condition.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
|
2007-06-06 04:53:16 +08:00
|
|
|
PN->reserveOperandSpace(2);
|
2007-06-16 07:05:46 +08:00
|
|
|
PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
|
2007-06-06 04:53:16 +08:00
|
|
|
PN->addIncoming(RHSCond, RHSBlock);
|
|
|
|
|
|
|
|
// ZExt result to int.
|
|
|
|
return RValue::get(Builder.CreateZExt(PN, LLVMIntTy, "lor.ext"));
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitBinaryAssign(const BinaryOperator *E) {
|
2007-08-09 01:43:05 +08:00
|
|
|
assert(E->getLHS()->getType().getCanonicalType() ==
|
|
|
|
E->getRHS()->getType().getCanonicalType() && "Invalid assignment");
|
2007-06-06 04:53:16 +08:00
|
|
|
LValue LHS = EmitLValue(E->getLHS());
|
2007-08-09 01:43:05 +08:00
|
|
|
RValue RHS = EmitExpr(E->getRHS());
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
// Store the value into the LHS.
|
|
|
|
EmitStoreThroughLValue(RHS, LHS, E->getType());
|
|
|
|
|
|
|
|
// Return the converted RHS.
|
|
|
|
return RHS;
|
|
|
|
}
|
|
|
|
|
2007-06-30 00:31:29 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
RValue CodeGenFunction::EmitBinaryComma(const BinaryOperator *E) {
|
|
|
|
EmitExpr(E->getLHS());
|
|
|
|
return EmitExpr(E->getRHS());
|
|
|
|
}
|
2007-07-13 13:18:11 +08:00
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator *E) {
|
|
|
|
llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
|
|
|
|
llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
|
|
|
|
llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
|
|
|
|
|
|
|
|
llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
|
|
|
|
Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
|
|
|
|
|
2007-07-14 08:01:01 +08:00
|
|
|
// FIXME: Implement this for aggregate values.
|
|
|
|
|
2007-07-13 13:18:11 +08:00
|
|
|
EmitBlock(LHSBlock);
|
2007-08-09 01:43:05 +08:00
|
|
|
// Handle the GNU extension for missing LHS.
|
|
|
|
llvm::Value *LHSValue = E->getLHS() ? EmitExpr(E->getLHS()).getVal() : Cond;
|
2007-07-13 13:18:11 +08:00
|
|
|
Builder.CreateBr(ContBlock);
|
|
|
|
LHSBlock = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
EmitBlock(RHSBlock);
|
2007-08-09 01:43:05 +08:00
|
|
|
|
|
|
|
llvm::Value *RHSValue = EmitExpr(E->getRHS()).getVal();
|
2007-07-13 13:18:11 +08:00
|
|
|
Builder.CreateBr(ContBlock);
|
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
const llvm::Type *LHSType = LHSValue->getType();
|
|
|
|
assert(LHSType == RHSValue->getType() && "?: LHS & RHS must have same type");
|
|
|
|
|
|
|
|
EmitBlock(ContBlock);
|
|
|
|
llvm::PHINode *PN = Builder.CreatePHI(LHSType, "cond");
|
|
|
|
PN->reserveOperandSpace(2);
|
|
|
|
PN->addIncoming(LHSValue, LHSBlock);
|
|
|
|
PN->addIncoming(RHSValue, RHSBlock);
|
|
|
|
|
|
|
|
return RValue::get(PN);
|
|
|
|
}
|