blob: dd3c2ed7f025e9502fea22943a778fb29c20dffa [file] [log] [blame]
Chris Lattner9fba49a2007-08-24 05:35:26 +00001//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes with scalar LLVM types as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
16#include "clang/AST/AST.h"
17#include "llvm/Constants.h"
18#include "llvm/Function.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/GlobalVariable.h"
Anders Carlsson36760332007-10-15 20:28:48 +000020#include "llvm/Intrinsics.h"
Chris Lattner9fba49a2007-08-24 05:35:26 +000021#include "llvm/Support/Compiler.h"
22using namespace clang;
23using namespace CodeGen;
24using llvm::Value;
25
26//===----------------------------------------------------------------------===//
27// Scalar Expression Emitter
28//===----------------------------------------------------------------------===//
29
30struct BinOpInfo {
31 Value *LHS;
32 Value *RHS;
Chris Lattner660e31d2007-08-24 21:00:35 +000033 QualType Ty; // Computation Type.
Chris Lattner9fba49a2007-08-24 05:35:26 +000034 const BinaryOperator *E;
35};
36
37namespace {
38class VISIBILITY_HIDDEN ScalarExprEmitter
39 : public StmtVisitor<ScalarExprEmitter, Value*> {
40 CodeGenFunction &CGF;
Devang Patel638b64c2007-10-09 19:49:58 +000041 llvm::LLVMFoldingBuilder &Builder;
Chris Lattner9fba49a2007-08-24 05:35:26 +000042public:
43
44 ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) {
45 }
46
47
48 //===--------------------------------------------------------------------===//
49 // Utilities
50 //===--------------------------------------------------------------------===//
51
52 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
53 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
54
55 Value *EmitLoadOfLValue(LValue LV, QualType T) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +000056 return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +000057 }
58
59 /// EmitLoadOfLValue - Given an expression with complex type that represents a
60 /// value l-value, this method emits the address of the l-value, then loads
61 /// and returns the result.
62 Value *EmitLoadOfLValue(const Expr *E) {
63 // FIXME: Volatile
64 return EmitLoadOfLValue(EmitLValue(E), E->getType());
65 }
66
Chris Lattnerd8d44222007-08-26 16:42:57 +000067 /// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +000068 /// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +000069 Value *EmitConversionToBool(Value *Src, QualType DstTy);
70
Chris Lattner4e05d1e2007-08-26 06:48:56 +000071 /// EmitScalarConversion - Emit a conversion from the specified type to the
72 /// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +000073 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
74
75 /// EmitComplexToScalarConversion - Emit a conversion from the specified
76 /// complex type to the specified destination type, where the destination
77 /// type is an LLVM scalar type.
78 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
79 QualType SrcTy, QualType DstTy);
Chris Lattner4e05d1e2007-08-26 06:48:56 +000080
Chris Lattner9fba49a2007-08-24 05:35:26 +000081 //===--------------------------------------------------------------------===//
82 // Visitor Methods
83 //===--------------------------------------------------------------------===//
84
85 Value *VisitStmt(Stmt *S) {
Chris Lattner1aef6212007-09-13 01:17:29 +000086 S->dump(CGF.getContext().SourceMgr);
Chris Lattner9fba49a2007-08-24 05:35:26 +000087 assert(0 && "Stmt can't have complex result type!");
88 return 0;
89 }
90 Value *VisitExpr(Expr *S);
91 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
92
93 // Leaves.
94 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
95 return llvm::ConstantInt::get(E->getValue());
96 }
97 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner7f298762007-09-22 18:47:25 +000098 return llvm::ConstantFP::get(ConvertType(E->getType()), E->getValue());
Chris Lattner9fba49a2007-08-24 05:35:26 +000099 }
100 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
101 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
102 }
103 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
104 return llvm::ConstantInt::get(ConvertType(E->getType()),
Steve Naroff85f0dc52007-10-15 20:41:53 +0000105 CGF.getContext().typesAreCompatible(
106 E->getArgType1(), E->getArgType2()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000107 }
108 Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
109 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
110 }
111
112 // l-values.
113 Value *VisitDeclRefExpr(DeclRefExpr *E) {
114 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
115 return llvm::ConstantInt::get(EC->getInitVal());
116 return EmitLoadOfLValue(E);
117 }
118 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
119 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
120 Value *VisitOCUVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
121 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); }
122 Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); }
Devang Patel01ab1302007-10-24 17:18:43 +0000123
124 Value *VisitInitListExpr(InitListExpr *E) {
Devang Patel01ab1302007-10-24 17:18:43 +0000125 unsigned N = E->getNumInits();
Devang Patel32c39832007-10-24 18:05:48 +0000126 QualType T = E->getInit(0)->getType();
127 Value *V = llvm::UndefValue::get(llvm::VectorType::get(ConvertType(T), N));
Devang Patel01ab1302007-10-24 17:18:43 +0000128 for (unsigned i = 0; i < N; ++i) {
Devang Patel32c39832007-10-24 18:05:48 +0000129 Value *NewV = Visit(E->getInit(i));
130 Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
131 V = Builder.CreateInsertElement(V, NewV, Idx);
Devang Patel01ab1302007-10-24 17:18:43 +0000132 }
Devang Patel32c39832007-10-24 18:05:48 +0000133 return V;
Devang Patel01ab1302007-10-24 17:18:43 +0000134 }
135
136 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
137 return Visit(E->getInitializer());
138 }
139
Chris Lattner9fba49a2007-08-24 05:35:26 +0000140 Value *VisitImplicitCastExpr(const ImplicitCastExpr *E);
141 Value *VisitCastExpr(const CastExpr *E) {
142 return EmitCastExpr(E->getSubExpr(), E->getType());
143 }
144 Value *EmitCastExpr(const Expr *E, QualType T);
145
146 Value *VisitCallExpr(const CallExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000147 return CGF.EmitCallExpr(E).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000148 }
149
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000150 Value *VisitStmtExpr(const StmtExpr *E);
151
Chris Lattner9fba49a2007-08-24 05:35:26 +0000152 // Unary Operators.
153 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre);
154 Value *VisitUnaryPostDec(const UnaryOperator *E) {
155 return VisitPrePostIncDec(E, false, false);
156 }
157 Value *VisitUnaryPostInc(const UnaryOperator *E) {
158 return VisitPrePostIncDec(E, true, false);
159 }
160 Value *VisitUnaryPreDec(const UnaryOperator *E) {
161 return VisitPrePostIncDec(E, false, true);
162 }
163 Value *VisitUnaryPreInc(const UnaryOperator *E) {
164 return VisitPrePostIncDec(E, true, true);
165 }
166 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
167 return EmitLValue(E->getSubExpr()).getAddress();
168 }
169 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
170 Value *VisitUnaryPlus(const UnaryOperator *E) {
171 return Visit(E->getSubExpr());
172 }
173 Value *VisitUnaryMinus (const UnaryOperator *E);
174 Value *VisitUnaryNot (const UnaryOperator *E);
175 Value *VisitUnaryLNot (const UnaryOperator *E);
176 Value *VisitUnarySizeOf (const UnaryOperator *E) {
177 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
178 }
179 Value *VisitUnaryAlignOf (const UnaryOperator *E) {
180 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
181 }
182 Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType,
183 bool isSizeOf);
Chris Lattner01211af2007-08-24 21:20:17 +0000184 Value *VisitUnaryReal (const UnaryOperator *E);
185 Value *VisitUnaryImag (const UnaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000186 Value *VisitUnaryExtension(const UnaryOperator *E) {
187 return Visit(E->getSubExpr());
188 }
189
190 // Binary Operators.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000191 Value *EmitMul(const BinOpInfo &Ops) {
192 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
193 }
194 Value *EmitDiv(const BinOpInfo &Ops);
195 Value *EmitRem(const BinOpInfo &Ops);
196 Value *EmitAdd(const BinOpInfo &Ops);
197 Value *EmitSub(const BinOpInfo &Ops);
198 Value *EmitShl(const BinOpInfo &Ops);
199 Value *EmitShr(const BinOpInfo &Ops);
200 Value *EmitAnd(const BinOpInfo &Ops) {
201 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
202 }
203 Value *EmitXor(const BinOpInfo &Ops) {
204 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
205 }
206 Value *EmitOr (const BinOpInfo &Ops) {
207 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
208 }
209
Chris Lattner660e31d2007-08-24 21:00:35 +0000210 BinOpInfo EmitBinOps(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000211 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000212 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
213
214 // Binary operators and binary compound assignment operators.
215#define HANDLEBINOP(OP) \
Chris Lattner0d965302007-08-26 21:41:21 +0000216 Value *VisitBin ## OP(const BinaryOperator *E) { \
217 return Emit ## OP(EmitBinOps(E)); \
218 } \
219 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
220 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
Chris Lattner660e31d2007-08-24 21:00:35 +0000221 }
222 HANDLEBINOP(Mul);
223 HANDLEBINOP(Div);
224 HANDLEBINOP(Rem);
225 HANDLEBINOP(Add);
226 // (Sub) - Sub is handled specially below for ptr-ptr subtract.
227 HANDLEBINOP(Shl);
228 HANDLEBINOP(Shr);
229 HANDLEBINOP(And);
230 HANDLEBINOP(Xor);
231 HANDLEBINOP(Or);
232#undef HANDLEBINOP
233 Value *VisitBinSub(const BinaryOperator *E);
Chris Lattner0d965302007-08-26 21:41:21 +0000234 Value *VisitBinSubAssign(const CompoundAssignOperator *E) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000235 return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub);
236 }
237
Chris Lattner9fba49a2007-08-24 05:35:26 +0000238 // Comparisons.
239 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
240 unsigned SICmpOpc, unsigned FCmpOpc);
241#define VISITCOMP(CODE, UI, SI, FP) \
242 Value *VisitBin##CODE(const BinaryOperator *E) { \
243 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
244 llvm::FCmpInst::FP); }
245 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT);
246 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT);
247 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE);
248 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE);
249 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ);
250 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE);
251#undef VISITCOMP
252
253 Value *VisitBinAssign (const BinaryOperator *E);
254
255 Value *VisitBinLAnd (const BinaryOperator *E);
256 Value *VisitBinLOr (const BinaryOperator *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000257 Value *VisitBinComma (const BinaryOperator *E);
258
259 // Other Operators.
260 Value *VisitConditionalOperator(const ConditionalOperator *CO);
261 Value *VisitChooseExpr(ChooseExpr *CE);
Anders Carlsson36760332007-10-15 20:28:48 +0000262 Value *VisitVAArgExpr(VAArgExpr *VE);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000263 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
264 return CGF.EmitObjCStringLiteral(E);
265 }
Anders Carlsson36f07d82007-10-29 05:01:08 +0000266 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000267};
268} // end anonymous namespace.
269
270//===----------------------------------------------------------------------===//
271// Utilities
272//===----------------------------------------------------------------------===//
273
Chris Lattnerd8d44222007-08-26 16:42:57 +0000274/// EmitConversionToBool - Convert the specified expression value to a
Chris Lattner05942062007-08-26 17:25:57 +0000275/// boolean (i1) truth value. This is equivalent to "Val != 0".
Chris Lattnerd8d44222007-08-26 16:42:57 +0000276Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
277 assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
278
279 if (SrcType->isRealFloatingType()) {
280 // Compare against 0.0 for fp scalars.
281 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
Chris Lattnerd8d44222007-08-26 16:42:57 +0000282 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
283 }
284
285 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
286 "Unknown scalar type to convert");
287
288 // Because of the type rules of C, we often end up computing a logical value,
289 // then zero extending it to int, then wanting it as a logical value again.
290 // Optimize this common case.
291 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
292 if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
293 Value *Result = ZI->getOperand(0);
294 ZI->eraseFromParent();
295 return Result;
296 }
297 }
298
299 // Compare against an integer or pointer null.
300 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
301 return Builder.CreateICmpNE(Src, Zero, "tobool");
302}
303
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000304/// EmitScalarConversion - Emit a conversion from the specified type to the
305/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000306Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
307 QualType DstType) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000308 SrcType = SrcType.getCanonicalType();
309 DstType = DstType.getCanonicalType();
310 if (SrcType == DstType) return Src;
Chris Lattnere133d7f2007-08-26 07:21:11 +0000311
312 if (DstType->isVoidType()) return 0;
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000313
314 // Handle conversions to bool first, they are special: comparisons against 0.
Chris Lattnerc39c3652007-08-26 16:52:28 +0000315 if (DstType->isBooleanType())
316 return EmitConversionToBool(Src, SrcType);
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000317
318 const llvm::Type *DstTy = ConvertType(DstType);
319
320 // Ignore conversions like int -> uint.
321 if (Src->getType() == DstTy)
322 return Src;
323
324 // Handle pointer conversions next: pointers can only be converted to/from
325 // other pointers and integers.
326 if (isa<PointerType>(DstType)) {
327 // The source value may be an integer, or a pointer.
328 if (isa<llvm::PointerType>(Src->getType()))
329 return Builder.CreateBitCast(Src, DstTy, "conv");
330 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
331 return Builder.CreateIntToPtr(Src, DstTy, "conv");
332 }
333
334 if (isa<PointerType>(SrcType)) {
335 // Must be an ptr to int cast.
336 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
337 return Builder.CreateIntToPtr(Src, DstTy, "conv");
338 }
339
340 // Finally, we have the arithmetic types: real int/float.
341 if (isa<llvm::IntegerType>(Src->getType())) {
342 bool InputSigned = SrcType->isSignedIntegerType();
343 if (isa<llvm::IntegerType>(DstTy))
344 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
345 else if (InputSigned)
346 return Builder.CreateSIToFP(Src, DstTy, "conv");
347 else
348 return Builder.CreateUIToFP(Src, DstTy, "conv");
349 }
350
351 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
352 if (isa<llvm::IntegerType>(DstTy)) {
353 if (DstType->isSignedIntegerType())
354 return Builder.CreateFPToSI(Src, DstTy, "conv");
355 else
356 return Builder.CreateFPToUI(Src, DstTy, "conv");
357 }
358
359 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
360 if (DstTy->getTypeID() < Src->getType()->getTypeID())
361 return Builder.CreateFPTrunc(Src, DstTy, "conv");
362 else
363 return Builder.CreateFPExt(Src, DstTy, "conv");
364}
365
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000366/// EmitComplexToScalarConversion - Emit a conversion from the specified
367/// complex type to the specified destination type, where the destination
368/// type is an LLVM scalar type.
369Value *ScalarExprEmitter::
370EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
371 QualType SrcTy, QualType DstTy) {
Chris Lattnerc39c3652007-08-26 16:52:28 +0000372 // Get the source element type.
373 SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType();
374
375 // Handle conversions to bool first, they are special: comparisons against 0.
376 if (DstTy->isBooleanType()) {
377 // Complex != 0 -> (Real != 0) | (Imag != 0)
378 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
379 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
380 return Builder.CreateOr(Src.first, Src.second, "tobool");
381 }
382
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000383 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
384 // the imaginary part of the complex value is discarded and the value of the
385 // real part is converted according to the conversion rules for the
386 // corresponding real type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000387 return EmitScalarConversion(Src.first, SrcTy, DstTy);
388}
389
390
Chris Lattner9fba49a2007-08-24 05:35:26 +0000391//===----------------------------------------------------------------------===//
392// Visitor Methods
393//===----------------------------------------------------------------------===//
394
395Value *ScalarExprEmitter::VisitExpr(Expr *E) {
396 fprintf(stderr, "Unimplemented scalar expr!\n");
Chris Lattner1aef6212007-09-13 01:17:29 +0000397 E->dump(CGF.getContext().SourceMgr);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000398 if (E->getType()->isVoidType())
399 return 0;
400 return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
401}
402
403Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
404 // Emit subscript expressions in rvalue context's. For most cases, this just
405 // loads the lvalue formed by the subscript expr. However, we have to be
406 // careful, because the base of a vector subscript is occasionally an rvalue,
407 // so we can't get it as an lvalue.
408 if (!E->getBase()->getType()->isVectorType())
409 return EmitLoadOfLValue(E);
410
411 // Handle the vector case. The base must be a vector, the index must be an
412 // integer value.
413 Value *Base = Visit(E->getBase());
414 Value *Idx = Visit(E->getIdx());
415
416 // FIXME: Convert Idx to i32 type.
417 return Builder.CreateExtractElement(Base, Idx, "vecext");
418}
419
420/// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but
421/// also handle things like function to pointer-to-function decay, and array to
422/// pointer decay.
423Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
424 const Expr *Op = E->getSubExpr();
425
426 // If this is due to array->pointer conversion, emit the array expression as
427 // an l-value.
428 if (Op->getType()->isArrayType()) {
429 // FIXME: For now we assume that all source arrays map to LLVM arrays. This
430 // will not true when we add support for VLAs.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000431 Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays.
Chris Lattner9fba49a2007-08-24 05:35:26 +0000432
433 assert(isa<llvm::PointerType>(V->getType()) &&
434 isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
435 ->getElementType()) &&
436 "Doesn't support VLAs yet!");
437 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Ted Kremenek7f6f4a42007-09-04 17:20:08 +0000438
439 llvm::Value *Ops[] = {Idx0, Idx0};
440 return Builder.CreateGEP(V, Ops, Ops+2, "arraydecay");
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000441 } else if (E->getType()->isReferenceType()) {
Anders Carlsson88842452007-10-13 05:52:34 +0000442 assert(cast<ReferenceType>(E->getType().getCanonicalType())->
443 getReferenceeType() ==
444 Op->getType().getCanonicalType() && "Incompatible types!");
Anders Carlssoncebb8d62007-10-12 23:56:29 +0000445
446 return EmitLValue(Op).getAddress();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000447 }
448
449 return EmitCastExpr(Op, E->getType());
450}
451
452
453// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
454// have to handle a more broad range of conversions than explicit casts, as they
455// handle things like function to ptr-to-function decay etc.
456Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) {
Chris Lattner82e10392007-08-26 07:26:12 +0000457 // Handle cases where the source is an non-complex type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000458 if (!E->getType()->isComplexType()) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000459 Value *Src = Visit(const_cast<Expr*>(E));
460
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000461 // Use EmitScalarConversion to perform the conversion.
462 return EmitScalarConversion(Src, E->getType(), DestTy);
463 }
Chris Lattnerd579f7f2007-08-26 07:16:41 +0000464
Chris Lattner82e10392007-08-26 07:26:12 +0000465 // Handle cases where the source is a complex type.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000466 return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(),
467 DestTy);
Chris Lattner9fba49a2007-08-24 05:35:26 +0000468}
469
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000470Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000471 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal();
Chris Lattnerea6cdd72007-08-31 22:09:40 +0000472}
473
474
Chris Lattner9fba49a2007-08-24 05:35:26 +0000475//===----------------------------------------------------------------------===//
476// Unary Operators
477//===----------------------------------------------------------------------===//
478
479Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattner855e3d72007-08-24 16:24:49 +0000480 bool isInc, bool isPre) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000481 LValue LV = EmitLValue(E->getSubExpr());
482 // FIXME: Handle volatile!
Chris Lattner0dc11f62007-08-26 05:10:16 +0000483 Value *InVal = CGF.EmitLoadOfLValue(LV, // false
Chris Lattnere24c4cf2007-08-31 22:49:20 +0000484 E->getSubExpr()->getType()).getScalarVal();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000485
486 int AmountVal = isInc ? 1 : -1;
487
488 Value *NextVal;
Chris Lattner0dc11f62007-08-26 05:10:16 +0000489 if (isa<llvm::PointerType>(InVal->getType())) {
490 // FIXME: This isn't right for VLAs.
491 NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
492 NextVal = Builder.CreateGEP(InVal, NextVal);
493 } else {
494 // Add the inc/dec to the real part.
495 if (isa<llvm::IntegerType>(InVal->getType()))
496 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000497 else if (InVal->getType() == llvm::Type::FloatTy)
498 // FIXME: Handle long double.
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000499 NextVal =
500 llvm::ConstantFP::get(InVal->getType(),
501 llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000502 else {
503 // FIXME: Handle long double.
504 assert(InVal->getType() == llvm::Type::DoubleTy);
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000505 NextVal =
506 llvm::ConstantFP::get(InVal->getType(),
507 llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerb2a7dab2007-09-13 06:19:18 +0000508 }
Chris Lattner0dc11f62007-08-26 05:10:16 +0000509 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
510 }
Chris Lattner9fba49a2007-08-24 05:35:26 +0000511
512 // Store the updated result through the lvalue.
513 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV,
514 E->getSubExpr()->getType());
515
516 // If this is a postinc, return the value read from memory, otherwise use the
517 // updated value.
518 return isPre ? NextVal : InVal;
519}
520
521
522Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
523 Value *Op = Visit(E->getSubExpr());
524 return Builder.CreateNeg(Op, "neg");
525}
526
527Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
528 Value *Op = Visit(E->getSubExpr());
529 return Builder.CreateNot(Op, "neg");
530}
531
532Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
533 // Compare operand to zero.
534 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
535
536 // Invert value.
537 // TODO: Could dynamically modify easy computations here. For example, if
538 // the operand is an icmp ne, turn into icmp eq.
539 BoolVal = Builder.CreateNot(BoolVal, "lnot");
540
541 // ZExt result to int.
542 return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext");
543}
544
545/// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as
546/// an integer (RetType).
547Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize,
Chris Lattner01211af2007-08-24 21:20:17 +0000548 QualType RetType,bool isSizeOf){
Chris Lattner9fba49a2007-08-24 05:35:26 +0000549 /// FIXME: This doesn't handle VLAs yet!
550 std::pair<uint64_t, unsigned> Info =
551 CGF.getContext().getTypeInfo(TypeToSize, SourceLocation());
552
553 uint64_t Val = isSizeOf ? Info.first : Info.second;
554 Val /= 8; // Return size in bytes, not bits.
555
556 assert(RetType->isIntegerType() && "Result type must be an integer!");
557
Hartmut Kaiserff08d2c2007-10-17 15:00:17 +0000558 uint32_t ResultWidth = static_cast<uint32_t>(
559 CGF.getContext().getTypeSize(RetType, SourceLocation()));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000560 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
561}
562
Chris Lattner01211af2007-08-24 21:20:17 +0000563Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
564 Expr *Op = E->getSubExpr();
565 if (Op->getType()->isComplexType())
566 return CGF.EmitComplexExpr(Op).first;
567 return Visit(Op);
568}
569Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
570 Expr *Op = E->getSubExpr();
571 if (Op->getType()->isComplexType())
572 return CGF.EmitComplexExpr(Op).second;
Chris Lattnerdb8a6c92007-08-26 05:29:21 +0000573
574 // __imag on a scalar returns zero. Emit it the subexpr to ensure side
575 // effects are evaluated.
576 CGF.EmitScalarExpr(Op);
577 return llvm::Constant::getNullValue(ConvertType(E->getType()));
Chris Lattner01211af2007-08-24 21:20:17 +0000578}
579
580
Chris Lattner9fba49a2007-08-24 05:35:26 +0000581//===----------------------------------------------------------------------===//
582// Binary Operators
583//===----------------------------------------------------------------------===//
584
585BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
586 BinOpInfo Result;
587 Result.LHS = Visit(E->getLHS());
588 Result.RHS = Visit(E->getRHS());
Chris Lattner660e31d2007-08-24 21:00:35 +0000589 Result.Ty = E->getType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000590 Result.E = E;
591 return Result;
592}
593
Chris Lattner0d965302007-08-26 21:41:21 +0000594Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
Chris Lattner660e31d2007-08-24 21:00:35 +0000595 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
596 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
597
598 BinOpInfo OpInfo;
599
600 // Load the LHS and RHS operands.
601 LValue LHSLV = EmitLValue(E->getLHS());
602 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000603
604 // Determine the computation type. If the RHS is complex, then this is one of
605 // the add/sub/mul/div operators. All of these operators can be computed in
606 // with just their real component even though the computation domain really is
607 // complex.
Chris Lattner0d965302007-08-26 21:41:21 +0000608 QualType ComputeType = E->getComputationType();
Chris Lattner660e31d2007-08-24 21:00:35 +0000609
Chris Lattner9c9f4bb2007-08-26 22:37:40 +0000610 // If the computation type is complex, then the RHS is complex. Emit the RHS.
611 if (const ComplexType *CT = ComputeType->getAsComplexType()) {
612 ComputeType = CT->getElementType();
613
614 // Emit the RHS, only keeping the real component.
615 OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first;
616 RHSTy = RHSTy->getAsComplexType()->getElementType();
617 } else {
618 // Otherwise the RHS is a simple scalar value.
619 OpInfo.RHS = Visit(E->getRHS());
620 }
621
622 // Convert the LHS/RHS values to the computation type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000623 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000624
Devang Patel04011802007-10-25 22:19:13 +0000625 // Do not merge types for -= or += where the LHS is a pointer.
626 if (!(E->getOpcode() == BinaryOperator::SubAssign ||
Devang Patelce6c8372007-10-30 18:31:12 +0000627 E->getOpcode() == BinaryOperator::AddAssign) ||
Chris Lattner42330c32007-08-25 21:56:20 +0000628 !E->getLHS()->getType()->isPointerType()) {
Chris Lattnerb1497062007-08-26 07:08:39 +0000629 OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType);
Chris Lattner660e31d2007-08-24 21:00:35 +0000630 }
631 OpInfo.Ty = ComputeType;
632 OpInfo.E = E;
633
634 // Expand the binary operator.
635 Value *Result = (this->*Func)(OpInfo);
636
637 // Truncate the result back to the LHS type.
Chris Lattnerb1497062007-08-26 07:08:39 +0000638 Result = EmitScalarConversion(Result, ComputeType, LHSTy);
Chris Lattner660e31d2007-08-24 21:00:35 +0000639
640 // Store the result value into the LHS lvalue.
641 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType());
642
643 return Result;
644}
645
646
Chris Lattner9fba49a2007-08-24 05:35:26 +0000647Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
648 if (Ops.LHS->getType()->isFloatingPoint())
649 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
Chris Lattner660e31d2007-08-24 21:00:35 +0000650 else if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000651 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
652 else
653 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
654}
655
656Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
657 // Rem in C can't be a floating point type: C99 6.5.5p2.
Chris Lattner660e31d2007-08-24 21:00:35 +0000658 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000659 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
660 else
661 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
662}
663
664
665Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
Chris Lattner660e31d2007-08-24 21:00:35 +0000666 if (!Ops.Ty->isPointerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000667 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
Chris Lattner660e31d2007-08-24 21:00:35 +0000668
669 // FIXME: What about a pointer to a VLA?
Chris Lattner9fba49a2007-08-24 05:35:26 +0000670 if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int
671 return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr");
672 // int + pointer
673 return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr");
674}
675
676Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
677 if (!isa<llvm::PointerType>(Ops.LHS->getType()))
678 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
679
Chris Lattner660e31d2007-08-24 21:00:35 +0000680 // pointer - int
681 assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
682 "ptr-ptr shouldn't get here");
683 // FIXME: The pointer could point to a VLA.
684 Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
685 return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr");
686}
687
688Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {
689 // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In
690 // the compound assignment case it is invalid, so just handle it here.
691 if (!E->getRHS()->getType()->isPointerType())
692 return EmitSub(EmitBinOps(E));
Chris Lattner9fba49a2007-08-24 05:35:26 +0000693
694 // pointer - pointer
Chris Lattner660e31d2007-08-24 21:00:35 +0000695 Value *LHS = Visit(E->getLHS());
696 Value *RHS = Visit(E->getRHS());
697
698 const PointerType *LHSPtrType = E->getLHS()->getType()->getAsPointerType();
699 assert(LHSPtrType == E->getRHS()->getType()->getAsPointerType() &&
700 "Can't subtract different pointer types");
701
Chris Lattner9fba49a2007-08-24 05:35:26 +0000702 QualType LHSElementType = LHSPtrType->getPointeeType();
Chris Lattner9fba49a2007-08-24 05:35:26 +0000703 uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType,
704 SourceLocation()) / 8;
Chris Lattner660e31d2007-08-24 21:00:35 +0000705
706 const llvm::Type *ResultType = ConvertType(E->getType());
707 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
708 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
709 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
Chris Lattner9fba49a2007-08-24 05:35:26 +0000710
711 // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
712 // remainder. As such, we handle common power-of-two cases here to generate
713 // better code.
714 if (llvm::isPowerOf2_64(ElementSize)) {
715 Value *ShAmt =
716 llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
717 return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
718 }
Chris Lattner660e31d2007-08-24 21:00:35 +0000719
Chris Lattner9fba49a2007-08-24 05:35:26 +0000720 // Otherwise, do a full sdiv.
721 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize);
722 return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
723}
724
Chris Lattner660e31d2007-08-24 21:00:35 +0000725
Chris Lattner9fba49a2007-08-24 05:35:26 +0000726Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
727 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
728 // RHS to the same size as the LHS.
729 Value *RHS = Ops.RHS;
730 if (Ops.LHS->getType() != RHS->getType())
731 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
732
733 return Builder.CreateShl(Ops.LHS, RHS, "shl");
734}
735
736Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
737 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
738 // RHS to the same size as the LHS.
739 Value *RHS = Ops.RHS;
740 if (Ops.LHS->getType() != RHS->getType())
741 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
742
Chris Lattner660e31d2007-08-24 21:00:35 +0000743 if (Ops.Ty->isUnsignedIntegerType())
Chris Lattner9fba49a2007-08-24 05:35:26 +0000744 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
745 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
746}
747
748Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
749 unsigned SICmpOpc, unsigned FCmpOpc) {
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000750 Value *Result;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000751 QualType LHSTy = E->getLHS()->getType();
752 if (!LHSTy->isComplexType()) {
753 Value *LHS = Visit(E->getLHS());
754 Value *RHS = Visit(E->getRHS());
755
756 if (LHS->getType()->isFloatingPoint()) {
757 Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
758 LHS, RHS, "cmp");
759 } else if (LHSTy->isUnsignedIntegerType()) {
760 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
761 LHS, RHS, "cmp");
762 } else {
763 // Signed integers and pointers.
764 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
765 LHS, RHS, "cmp");
766 }
767 } else {
768 // Complex Comparison: can only be an equality comparison.
769 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
770 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
771
772 QualType CETy =
773 cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
774
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000775 Value *ResultR, *ResultI;
Chris Lattner9fba49a2007-08-24 05:35:26 +0000776 if (CETy->isRealFloatingType()) {
777 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
778 LHS.first, RHS.first, "cmp.r");
779 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
780 LHS.second, RHS.second, "cmp.i");
781 } else {
782 // Complex comparisons can only be equality comparisons. As such, signed
783 // and unsigned opcodes are the same.
784 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
785 LHS.first, RHS.first, "cmp.r");
786 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
787 LHS.second, RHS.second, "cmp.i");
788 }
789
790 if (E->getOpcode() == BinaryOperator::EQ) {
791 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
792 } else {
793 assert(E->getOpcode() == BinaryOperator::NE &&
794 "Complex comparison other than == or != ?");
795 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
796 }
797 }
798
799 // ZExt result to int.
800 return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext");
801}
802
803Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
804 LValue LHS = EmitLValue(E->getLHS());
805 Value *RHS = Visit(E->getRHS());
806
807 // Store the value into the LHS.
808 // FIXME: Volatility!
809 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
810
811 // Return the RHS.
812 return RHS;
813}
814
815Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
816 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
817
818 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont");
819 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs");
820
821 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
822 Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock);
823
824 CGF.EmitBlock(RHSBlock);
825 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
826
827 // Reaquire the RHS block, as there may be subblocks inserted.
828 RHSBlock = Builder.GetInsertBlock();
829 CGF.EmitBlock(ContBlock);
830
831 // Create a PHI node. If we just evaluted the LHS condition, the result is
832 // false. If we evaluated both, the result is the RHS condition.
833 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land");
834 PN->reserveOperandSpace(2);
835 PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock);
836 PN->addIncoming(RHSCond, RHSBlock);
837
838 // ZExt result to int.
839 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
840}
841
842Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
843 Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS());
844
845 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont");
846 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs");
847
848 llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock();
849 Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock);
850
851 CGF.EmitBlock(RHSBlock);
852 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
853
854 // Reaquire the RHS block, as there may be subblocks inserted.
855 RHSBlock = Builder.GetInsertBlock();
856 CGF.EmitBlock(ContBlock);
857
858 // Create a PHI node. If we just evaluted the LHS condition, the result is
859 // true. If we evaluated both, the result is the RHS condition.
860 llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor");
861 PN->reserveOperandSpace(2);
862 PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock);
863 PN->addIncoming(RHSCond, RHSBlock);
864
865 // ZExt result to int.
866 return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
867}
868
869Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
870 CGF.EmitStmt(E->getLHS());
871 return Visit(E->getRHS());
872}
873
874//===----------------------------------------------------------------------===//
875// Other Operators
876//===----------------------------------------------------------------------===//
877
878Value *ScalarExprEmitter::
879VisitConditionalOperator(const ConditionalOperator *E) {
880 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
881 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
882 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
883
884 Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
885 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
886
887 CGF.EmitBlock(LHSBlock);
888
889 // Handle the GNU extension for missing LHS.
890 Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond;
891 Builder.CreateBr(ContBlock);
892 LHSBlock = Builder.GetInsertBlock();
893
894 CGF.EmitBlock(RHSBlock);
895
896 Value *RHS = Visit(E->getRHS());
897 Builder.CreateBr(ContBlock);
898 RHSBlock = Builder.GetInsertBlock();
899
900 CGF.EmitBlock(ContBlock);
901
902 // Create a PHI node for the real part.
903 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
904 PN->reserveOperandSpace(2);
905 PN->addIncoming(LHS, LHSBlock);
906 PN->addIncoming(RHS, RHSBlock);
907 return PN;
908}
909
910Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattner9fba49a2007-08-24 05:35:26 +0000911 // Emit the LHS or RHS as appropriate.
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000912 return
913 Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS());
Chris Lattner9fba49a2007-08-24 05:35:26 +0000914}
915
Anders Carlsson36760332007-10-15 20:28:48 +0000916Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE)
917{
918 llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress();
919
920 llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
921 return V;
922}
923
Anders Carlsson36f07d82007-10-29 05:01:08 +0000924Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E)
925{
926 std::string str;
927
928 CGF.getContext().getObjcEncodingForType(E->getEncodedType(), str);
929
930 llvm::Constant *C = llvm::ConstantArray::get(str);
931 C = new llvm::GlobalVariable(C->getType(), true,
932 llvm::GlobalValue::InternalLinkage,
933 C, ".str", &CGF.CGM.getModule());
934 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
935 llvm::Constant *Zeros[] = { Zero, Zero };
936 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
937
938 return C;
939}
940
Chris Lattner9fba49a2007-08-24 05:35:26 +0000941//===----------------------------------------------------------------------===//
942// Entry Point into this File
943//===----------------------------------------------------------------------===//
944
945/// EmitComplexExpr - Emit the computation of the specified expression of
946/// complex type, ignoring the result.
947Value *CodeGenFunction::EmitScalarExpr(const Expr *E) {
948 assert(E && !hasAggregateLLVMType(E->getType()) &&
949 "Invalid scalar expression to emit");
950
951 return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E));
952}
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000953
954/// EmitScalarConversion - Emit a conversion from the specified type to the
955/// specified destination type, both of which are LLVM scalar types.
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000956Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
957 QualType DstTy) {
Chris Lattner4e05d1e2007-08-26 06:48:56 +0000958 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
959 "Invalid scalar expression to emit");
960 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
961}
Chris Lattnerfb182ee2007-08-26 16:34:22 +0000962
963/// EmitComplexToScalarConversion - Emit a conversion from the specified
964/// complex type to the specified destination type, where the destination
965/// type is an LLVM scalar type.
966Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
967 QualType SrcTy,
968 QualType DstTy) {
969 assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) &&
970 "Invalid complex -> scalar conversion");
971 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
972 DstTy);
973}