Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1 | //===--- 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 Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame^] | 19 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 20 | #include "llvm/Intrinsics.h" |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
| 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | using llvm::Value; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Scalar Expression Emitter |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | struct BinOpInfo { |
| 31 | Value *LHS; |
| 32 | Value *RHS; |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 33 | QualType Ty; // Computation Type. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 34 | const BinaryOperator *E; |
| 35 | }; |
| 36 | |
| 37 | namespace { |
| 38 | class VISIBILITY_HIDDEN ScalarExprEmitter |
| 39 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 40 | CodeGenFunction &CGF; |
Devang Patel | 638b64c | 2007-10-09 19:49:58 +0000 | [diff] [blame] | 41 | llvm::LLVMFoldingBuilder &Builder; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 42 | public: |
| 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 Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 56 | return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 57 | } |
| 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 Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 67 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 0594206 | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 68 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 69 | Value *EmitConversionToBool(Value *Src, QualType DstTy); |
| 70 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 71 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 72 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 73 | 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 Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 81 | //===--------------------------------------------------------------------===// |
| 82 | // Visitor Methods |
| 83 | //===--------------------------------------------------------------------===// |
| 84 | |
| 85 | Value *VisitStmt(Stmt *S) { |
Chris Lattner | 1aef621 | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 86 | S->dump(CGF.getContext().SourceMgr); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 87 | 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 Lattner | 7f29876 | 2007-09-22 18:47:25 +0000 | [diff] [blame] | 98 | return llvm::ConstantFP::get(ConvertType(E->getType()), E->getValue()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 99 | } |
| 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 Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 105 | CGF.getContext().typesAreCompatible( |
| 106 | E->getArgType1(), E->getArgType2())); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 107 | } |
| 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 Patel | 01ab130 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 123 | |
| 124 | Value *VisitInitListExpr(InitListExpr *E) { |
Devang Patel | 01ab130 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 125 | unsigned N = E->getNumInits(); |
Devang Patel | 32c3983 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 126 | QualType T = E->getInit(0)->getType(); |
| 127 | Value *V = llvm::UndefValue::get(llvm::VectorType::get(ConvertType(T), N)); |
Devang Patel | 01ab130 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 128 | for (unsigned i = 0; i < N; ++i) { |
Devang Patel | 32c3983 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 129 | Value *NewV = Visit(E->getInit(i)); |
| 130 | Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 131 | V = Builder.CreateInsertElement(V, NewV, Idx); |
Devang Patel | 01ab130 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 132 | } |
Devang Patel | 32c3983 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 133 | return V; |
Devang Patel | 01ab130 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 137 | return Visit(E->getInitializer()); |
| 138 | } |
| 139 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 140 | 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 Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 147 | return CGF.EmitCallExpr(E).getScalarVal(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 150 | Value *VisitStmtExpr(const StmtExpr *E); |
| 151 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 152 | // 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 Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 184 | Value *VisitUnaryReal (const UnaryOperator *E); |
| 185 | Value *VisitUnaryImag (const UnaryOperator *E); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 186 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 187 | return Visit(E->getSubExpr()); |
| 188 | } |
| 189 | |
| 190 | // Binary Operators. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 191 | 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 Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 210 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
Chris Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 211 | Value *EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 212 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); |
| 213 | |
| 214 | // Binary operators and binary compound assignment operators. |
| 215 | #define HANDLEBINOP(OP) \ |
Chris Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 216 | 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 Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 221 | } |
| 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 Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 234 | Value *VisitBinSubAssign(const CompoundAssignOperator *E) { |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 235 | return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub); |
| 236 | } |
| 237 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 238 | // 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 Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 257 | Value *VisitBinComma (const BinaryOperator *E); |
| 258 | |
| 259 | // Other Operators. |
| 260 | Value *VisitConditionalOperator(const ConditionalOperator *CO); |
| 261 | Value *VisitChooseExpr(ChooseExpr *CE); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 262 | Value *VisitVAArgExpr(VAArgExpr *VE); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 263 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 264 | return CGF.EmitObjCStringLiteral(E); |
| 265 | } |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame^] | 266 | Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 267 | }; |
| 268 | } // end anonymous namespace. |
| 269 | |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | // Utilities |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 274 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 0594206 | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 275 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 276 | Value *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 Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 282 | 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 Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 304 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 305 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 306 | Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, |
| 307 | QualType DstType) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 308 | SrcType = SrcType.getCanonicalType(); |
| 309 | DstType = DstType.getCanonicalType(); |
| 310 | if (SrcType == DstType) return Src; |
Chris Lattner | e133d7f | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 311 | |
| 312 | if (DstType->isVoidType()) return 0; |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 313 | |
| 314 | // Handle conversions to bool first, they are special: comparisons against 0. |
Chris Lattner | c39c365 | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 315 | if (DstType->isBooleanType()) |
| 316 | return EmitConversionToBool(Src, SrcType); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 317 | |
| 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 Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 366 | /// 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. |
| 369 | Value *ScalarExprEmitter:: |
| 370 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 371 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | c39c365 | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 372 | // 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 Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 383 | // 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 Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 387 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 388 | } |
| 389 | |
| 390 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 391 | //===----------------------------------------------------------------------===// |
| 392 | // Visitor Methods |
| 393 | //===----------------------------------------------------------------------===// |
| 394 | |
| 395 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
| 396 | fprintf(stderr, "Unimplemented scalar expr!\n"); |
Chris Lattner | 1aef621 | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 397 | E->dump(CGF.getContext().SourceMgr); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 398 | if (E->getType()->isVoidType()) |
| 399 | return 0; |
| 400 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
| 401 | } |
| 402 | |
| 403 | Value *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. |
| 423 | Value *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 Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 431 | Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 432 | |
| 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 Kremenek | 7f6f4a4 | 2007-09-04 17:20:08 +0000 | [diff] [blame] | 438 | |
| 439 | llvm::Value *Ops[] = {Idx0, Idx0}; |
| 440 | return Builder.CreateGEP(V, Ops, Ops+2, "arraydecay"); |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 441 | } else if (E->getType()->isReferenceType()) { |
Anders Carlsson | 8884245 | 2007-10-13 05:52:34 +0000 | [diff] [blame] | 442 | assert(cast<ReferenceType>(E->getType().getCanonicalType())-> |
| 443 | getReferenceeType() == |
| 444 | Op->getType().getCanonicalType() && "Incompatible types!"); |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 445 | |
| 446 | return EmitLValue(Op).getAddress(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 447 | } |
| 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. |
| 456 | Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) { |
Chris Lattner | 82e1039 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 457 | // Handle cases where the source is an non-complex type. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 458 | if (!E->getType()->isComplexType()) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 459 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 460 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 461 | // Use EmitScalarConversion to perform the conversion. |
| 462 | return EmitScalarConversion(Src, E->getType(), DestTy); |
| 463 | } |
Chris Lattner | d579f7f | 2007-08-26 07:16:41 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 82e1039 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 465 | // Handle cases where the source is a complex type. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 466 | return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(), |
| 467 | DestTy); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 470 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 471 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal(); |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 475 | //===----------------------------------------------------------------------===// |
| 476 | // Unary Operators |
| 477 | //===----------------------------------------------------------------------===// |
| 478 | |
| 479 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | 855e3d7 | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 480 | bool isInc, bool isPre) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 481 | LValue LV = EmitLValue(E->getSubExpr()); |
| 482 | // FIXME: Handle volatile! |
Chris Lattner | 0dc11f6 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 483 | Value *InVal = CGF.EmitLoadOfLValue(LV, // false |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 484 | E->getSubExpr()->getType()).getScalarVal(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 485 | |
| 486 | int AmountVal = isInc ? 1 : -1; |
| 487 | |
| 488 | Value *NextVal; |
Chris Lattner | 0dc11f6 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 489 | 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 Lattner | b2a7dab | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 497 | else if (InVal->getType() == llvm::Type::FloatTy) |
| 498 | // FIXME: Handle long double. |
| 499 | NextVal = llvm::ConstantFP::get(InVal->getType(), |
| 500 | llvm::APFloat(static_cast<float>(AmountVal))); |
| 501 | else { |
| 502 | // FIXME: Handle long double. |
| 503 | assert(InVal->getType() == llvm::Type::DoubleTy); |
| 504 | NextVal = llvm::ConstantFP::get(InVal->getType(), |
| 505 | llvm::APFloat(static_cast<double>(AmountVal))); |
| 506 | } |
Chris Lattner | 0dc11f6 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 507 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 508 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 509 | |
| 510 | // Store the updated result through the lvalue. |
| 511 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, |
| 512 | E->getSubExpr()->getType()); |
| 513 | |
| 514 | // If this is a postinc, return the value read from memory, otherwise use the |
| 515 | // updated value. |
| 516 | return isPre ? NextVal : InVal; |
| 517 | } |
| 518 | |
| 519 | |
| 520 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 521 | Value *Op = Visit(E->getSubExpr()); |
| 522 | return Builder.CreateNeg(Op, "neg"); |
| 523 | } |
| 524 | |
| 525 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 526 | Value *Op = Visit(E->getSubExpr()); |
| 527 | return Builder.CreateNot(Op, "neg"); |
| 528 | } |
| 529 | |
| 530 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 531 | // Compare operand to zero. |
| 532 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
| 533 | |
| 534 | // Invert value. |
| 535 | // TODO: Could dynamically modify easy computations here. For example, if |
| 536 | // the operand is an icmp ne, turn into icmp eq. |
| 537 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 538 | |
| 539 | // ZExt result to int. |
| 540 | return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext"); |
| 541 | } |
| 542 | |
| 543 | /// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as |
| 544 | /// an integer (RetType). |
| 545 | Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize, |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 546 | QualType RetType,bool isSizeOf){ |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 547 | /// FIXME: This doesn't handle VLAs yet! |
| 548 | std::pair<uint64_t, unsigned> Info = |
| 549 | CGF.getContext().getTypeInfo(TypeToSize, SourceLocation()); |
| 550 | |
| 551 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 552 | Val /= 8; // Return size in bytes, not bits. |
| 553 | |
| 554 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 555 | |
Hartmut Kaiser | ff08d2c | 2007-10-17 15:00:17 +0000 | [diff] [blame] | 556 | uint32_t ResultWidth = static_cast<uint32_t>( |
| 557 | CGF.getContext().getTypeSize(RetType, SourceLocation())); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 558 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 559 | } |
| 560 | |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 561 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 562 | Expr *Op = E->getSubExpr(); |
| 563 | if (Op->getType()->isComplexType()) |
| 564 | return CGF.EmitComplexExpr(Op).first; |
| 565 | return Visit(Op); |
| 566 | } |
| 567 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 568 | Expr *Op = E->getSubExpr(); |
| 569 | if (Op->getType()->isComplexType()) |
| 570 | return CGF.EmitComplexExpr(Op).second; |
Chris Lattner | db8a6c9 | 2007-08-26 05:29:21 +0000 | [diff] [blame] | 571 | |
| 572 | // __imag on a scalar returns zero. Emit it the subexpr to ensure side |
| 573 | // effects are evaluated. |
| 574 | CGF.EmitScalarExpr(Op); |
| 575 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 579 | //===----------------------------------------------------------------------===// |
| 580 | // Binary Operators |
| 581 | //===----------------------------------------------------------------------===// |
| 582 | |
| 583 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
| 584 | BinOpInfo Result; |
| 585 | Result.LHS = Visit(E->getLHS()); |
| 586 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 587 | Result.Ty = E->getType(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 588 | Result.E = E; |
| 589 | return Result; |
| 590 | } |
| 591 | |
Chris Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 592 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 593 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
| 594 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 595 | |
| 596 | BinOpInfo OpInfo; |
| 597 | |
| 598 | // Load the LHS and RHS operands. |
| 599 | LValue LHSLV = EmitLValue(E->getLHS()); |
| 600 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Chris Lattner | 9c9f4bb | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 601 | |
| 602 | // Determine the computation type. If the RHS is complex, then this is one of |
| 603 | // the add/sub/mul/div operators. All of these operators can be computed in |
| 604 | // with just their real component even though the computation domain really is |
| 605 | // complex. |
Chris Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 606 | QualType ComputeType = E->getComputationType(); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 607 | |
Chris Lattner | 9c9f4bb | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 608 | // If the computation type is complex, then the RHS is complex. Emit the RHS. |
| 609 | if (const ComplexType *CT = ComputeType->getAsComplexType()) { |
| 610 | ComputeType = CT->getElementType(); |
| 611 | |
| 612 | // Emit the RHS, only keeping the real component. |
| 613 | OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first; |
| 614 | RHSTy = RHSTy->getAsComplexType()->getElementType(); |
| 615 | } else { |
| 616 | // Otherwise the RHS is a simple scalar value. |
| 617 | OpInfo.RHS = Visit(E->getRHS()); |
| 618 | } |
| 619 | |
| 620 | // Convert the LHS/RHS values to the computation type. |
Chris Lattner | b149706 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 621 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 622 | |
Devang Patel | 0401180 | 2007-10-25 22:19:13 +0000 | [diff] [blame] | 623 | // Do not merge types for -= or += where the LHS is a pointer. |
| 624 | if (!(E->getOpcode() == BinaryOperator::SubAssign || |
| 625 | E->getOpcode() == BinaryOperator::AddAssign) || |
| 626 | // if (E->getOpcode() != BinaryOperator::SubAssign || |
Chris Lattner | 42330c3 | 2007-08-25 21:56:20 +0000 | [diff] [blame] | 627 | !E->getLHS()->getType()->isPointerType()) { |
Chris Lattner | b149706 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 628 | OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 629 | } |
| 630 | OpInfo.Ty = ComputeType; |
| 631 | OpInfo.E = E; |
| 632 | |
| 633 | // Expand the binary operator. |
| 634 | Value *Result = (this->*Func)(OpInfo); |
| 635 | |
| 636 | // Truncate the result back to the LHS type. |
Chris Lattner | b149706 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 637 | Result = EmitScalarConversion(Result, ComputeType, LHSTy); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 638 | |
| 639 | // Store the result value into the LHS lvalue. |
| 640 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType()); |
| 641 | |
| 642 | return Result; |
| 643 | } |
| 644 | |
| 645 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 646 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
| 647 | if (Ops.LHS->getType()->isFloatingPoint()) |
| 648 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 649 | else if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 650 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 651 | else |
| 652 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 653 | } |
| 654 | |
| 655 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 656 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 657 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 658 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 659 | else |
| 660 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 661 | } |
| 662 | |
| 663 | |
| 664 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 665 | if (!Ops.Ty->isPointerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 666 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 667 | |
| 668 | // FIXME: What about a pointer to a VLA? |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 669 | if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int |
| 670 | return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr"); |
| 671 | // int + pointer |
| 672 | return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr"); |
| 673 | } |
| 674 | |
| 675 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
| 676 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) |
| 677 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
| 678 | |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 679 | // pointer - int |
| 680 | assert(!isa<llvm::PointerType>(Ops.RHS->getType()) && |
| 681 | "ptr-ptr shouldn't get here"); |
| 682 | // FIXME: The pointer could point to a VLA. |
| 683 | Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg"); |
| 684 | return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr"); |
| 685 | } |
| 686 | |
| 687 | Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) { |
| 688 | // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In |
| 689 | // the compound assignment case it is invalid, so just handle it here. |
| 690 | if (!E->getRHS()->getType()->isPointerType()) |
| 691 | return EmitSub(EmitBinOps(E)); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 692 | |
| 693 | // pointer - pointer |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 694 | Value *LHS = Visit(E->getLHS()); |
| 695 | Value *RHS = Visit(E->getRHS()); |
| 696 | |
| 697 | const PointerType *LHSPtrType = E->getLHS()->getType()->getAsPointerType(); |
| 698 | assert(LHSPtrType == E->getRHS()->getType()->getAsPointerType() && |
| 699 | "Can't subtract different pointer types"); |
| 700 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 701 | QualType LHSElementType = LHSPtrType->getPointeeType(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 702 | uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType, |
| 703 | SourceLocation()) / 8; |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 704 | |
| 705 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 706 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 707 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 708 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 709 | |
| 710 | // HACK: LLVM doesn't have an divide instruction that 'knows' there is no |
| 711 | // remainder. As such, we handle common power-of-two cases here to generate |
| 712 | // better code. |
| 713 | if (llvm::isPowerOf2_64(ElementSize)) { |
| 714 | Value *ShAmt = |
| 715 | llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize)); |
| 716 | return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr"); |
| 717 | } |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 718 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 719 | // Otherwise, do a full sdiv. |
| 720 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
| 721 | return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
| 722 | } |
| 723 | |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 725 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 726 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 727 | // RHS to the same size as the LHS. |
| 728 | Value *RHS = Ops.RHS; |
| 729 | if (Ops.LHS->getType() != RHS->getType()) |
| 730 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 731 | |
| 732 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 733 | } |
| 734 | |
| 735 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 736 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 737 | // RHS to the same size as the LHS. |
| 738 | Value *RHS = Ops.RHS; |
| 739 | if (Ops.LHS->getType() != RHS->getType()) |
| 740 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 741 | |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 742 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 743 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 744 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 745 | } |
| 746 | |
| 747 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 748 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 749 | Value *Result; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 750 | QualType LHSTy = E->getLHS()->getType(); |
| 751 | if (!LHSTy->isComplexType()) { |
| 752 | Value *LHS = Visit(E->getLHS()); |
| 753 | Value *RHS = Visit(E->getRHS()); |
| 754 | |
| 755 | if (LHS->getType()->isFloatingPoint()) { |
| 756 | Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 757 | LHS, RHS, "cmp"); |
| 758 | } else if (LHSTy->isUnsignedIntegerType()) { |
| 759 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 760 | LHS, RHS, "cmp"); |
| 761 | } else { |
| 762 | // Signed integers and pointers. |
| 763 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
| 764 | LHS, RHS, "cmp"); |
| 765 | } |
| 766 | } else { |
| 767 | // Complex Comparison: can only be an equality comparison. |
| 768 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 769 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
| 770 | |
| 771 | QualType CETy = |
| 772 | cast<ComplexType>(LHSTy.getCanonicalType())->getElementType(); |
| 773 | |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 774 | Value *ResultR, *ResultI; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 775 | if (CETy->isRealFloatingType()) { |
| 776 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 777 | LHS.first, RHS.first, "cmp.r"); |
| 778 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 779 | LHS.second, RHS.second, "cmp.i"); |
| 780 | } else { |
| 781 | // Complex comparisons can only be equality comparisons. As such, signed |
| 782 | // and unsigned opcodes are the same. |
| 783 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 784 | LHS.first, RHS.first, "cmp.r"); |
| 785 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 786 | LHS.second, RHS.second, "cmp.i"); |
| 787 | } |
| 788 | |
| 789 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 790 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 791 | } else { |
| 792 | assert(E->getOpcode() == BinaryOperator::NE && |
| 793 | "Complex comparison other than == or != ?"); |
| 794 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | // ZExt result to int. |
| 799 | return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext"); |
| 800 | } |
| 801 | |
| 802 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 803 | LValue LHS = EmitLValue(E->getLHS()); |
| 804 | Value *RHS = Visit(E->getRHS()); |
| 805 | |
| 806 | // Store the value into the LHS. |
| 807 | // FIXME: Volatility! |
| 808 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
| 809 | |
| 810 | // Return the RHS. |
| 811 | return RHS; |
| 812 | } |
| 813 | |
| 814 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
| 815 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 816 | |
| 817 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont"); |
| 818 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs"); |
| 819 | |
| 820 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 821 | Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock); |
| 822 | |
| 823 | CGF.EmitBlock(RHSBlock); |
| 824 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 825 | |
| 826 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 827 | RHSBlock = Builder.GetInsertBlock(); |
| 828 | CGF.EmitBlock(ContBlock); |
| 829 | |
| 830 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 831 | // false. If we evaluated both, the result is the RHS condition. |
| 832 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land"); |
| 833 | PN->reserveOperandSpace(2); |
| 834 | PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock); |
| 835 | PN->addIncoming(RHSCond, RHSBlock); |
| 836 | |
| 837 | // ZExt result to int. |
| 838 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext"); |
| 839 | } |
| 840 | |
| 841 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
| 842 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 843 | |
| 844 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont"); |
| 845 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs"); |
| 846 | |
| 847 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 848 | Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock); |
| 849 | |
| 850 | CGF.EmitBlock(RHSBlock); |
| 851 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 852 | |
| 853 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 854 | RHSBlock = Builder.GetInsertBlock(); |
| 855 | CGF.EmitBlock(ContBlock); |
| 856 | |
| 857 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 858 | // true. If we evaluated both, the result is the RHS condition. |
| 859 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor"); |
| 860 | PN->reserveOperandSpace(2); |
| 861 | PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock); |
| 862 | PN->addIncoming(RHSCond, RHSBlock); |
| 863 | |
| 864 | // ZExt result to int. |
| 865 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext"); |
| 866 | } |
| 867 | |
| 868 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 869 | CGF.EmitStmt(E->getLHS()); |
| 870 | return Visit(E->getRHS()); |
| 871 | } |
| 872 | |
| 873 | //===----------------------------------------------------------------------===// |
| 874 | // Other Operators |
| 875 | //===----------------------------------------------------------------------===// |
| 876 | |
| 877 | Value *ScalarExprEmitter:: |
| 878 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 879 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 880 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 881 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 882 | |
| 883 | Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
| 884 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
| 885 | |
| 886 | CGF.EmitBlock(LHSBlock); |
| 887 | |
| 888 | // Handle the GNU extension for missing LHS. |
| 889 | Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond; |
| 890 | Builder.CreateBr(ContBlock); |
| 891 | LHSBlock = Builder.GetInsertBlock(); |
| 892 | |
| 893 | CGF.EmitBlock(RHSBlock); |
| 894 | |
| 895 | Value *RHS = Visit(E->getRHS()); |
| 896 | Builder.CreateBr(ContBlock); |
| 897 | RHSBlock = Builder.GetInsertBlock(); |
| 898 | |
| 899 | CGF.EmitBlock(ContBlock); |
| 900 | |
| 901 | // Create a PHI node for the real part. |
| 902 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 903 | PN->reserveOperandSpace(2); |
| 904 | PN->addIncoming(LHS, LHSBlock); |
| 905 | PN->addIncoming(RHS, RHSBlock); |
| 906 | return PN; |
| 907 | } |
| 908 | |
| 909 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 910 | // Emit the LHS or RHS as appropriate. |
Chris Lattner | f624cd2 | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 911 | return Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 914 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) |
| 915 | { |
| 916 | llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress(); |
| 917 | |
| 918 | llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 919 | return V; |
| 920 | } |
| 921 | |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame^] | 922 | Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) |
| 923 | { |
| 924 | std::string str; |
| 925 | |
| 926 | CGF.getContext().getObjcEncodingForType(E->getEncodedType(), str); |
| 927 | |
| 928 | llvm::Constant *C = llvm::ConstantArray::get(str); |
| 929 | C = new llvm::GlobalVariable(C->getType(), true, |
| 930 | llvm::GlobalValue::InternalLinkage, |
| 931 | C, ".str", &CGF.CGM.getModule()); |
| 932 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 933 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 934 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 935 | |
| 936 | return C; |
| 937 | } |
| 938 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 939 | //===----------------------------------------------------------------------===// |
| 940 | // Entry Point into this File |
| 941 | //===----------------------------------------------------------------------===// |
| 942 | |
| 943 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 944 | /// complex type, ignoring the result. |
| 945 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E) { |
| 946 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 947 | "Invalid scalar expression to emit"); |
| 948 | |
| 949 | return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
| 950 | } |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 951 | |
| 952 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 953 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 954 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 955 | QualType DstTy) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 956 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 957 | "Invalid scalar expression to emit"); |
| 958 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 959 | } |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 960 | |
| 961 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 962 | /// complex type to the specified destination type, where the destination |
| 963 | /// type is an LLVM scalar type. |
| 964 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 965 | QualType SrcTy, |
| 966 | QualType DstTy) { |
| 967 | assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) && |
| 968 | "Invalid complex -> scalar conversion"); |
| 969 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 970 | DstTy); |
| 971 | } |