blob: f465907735ea139205d14e9288fa934e79f58122 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000032User::op_iterator CallSite::getCallee() const {
33 Instruction *II(getInstruction());
34 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000035 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000036 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037}
38
Gordon Henriksen14a55692007-12-10 02:14:30 +000039//===----------------------------------------------------------------------===//
40// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43// Out of line virtual method, so the vtable, etc has a home.
44TerminatorInst::~TerminatorInst() {
45}
46
Gabor Greiff6caff662008-05-10 08:32:32 +000047//===----------------------------------------------------------------------===//
48// UnaryInstruction Class
49//===----------------------------------------------------------------------===//
50
Gordon Henriksen14a55692007-12-10 02:14:30 +000051// Out of line virtual method, so the vtable, etc has a home.
52UnaryInstruction::~UnaryInstruction() {
53}
54
Chris Lattnerafdb3de2005-01-29 00:35:16 +000055//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000056// SelectInst Class
57//===----------------------------------------------------------------------===//
58
59/// areInvalidOperands - Return a string if the specified operands are invalid
60/// for a select operation, otherwise return null.
61const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
62 if (Op1->getType() != Op2->getType())
63 return "both values to select must have same type";
64
65 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
66 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000067 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000068 return "vector select condition element type must be i1";
69 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
70 if (ET == 0)
71 return "selected values for vector select must be vectors";
72 if (ET->getNumElements() != VT->getNumElements())
73 return "vector select requires selected vectors to have "
74 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000075 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000076 return "select condition must be i1 or <n x i1>";
77 }
78 return 0;
79}
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000083// PHINode Class
84//===----------------------------------------------------------------------===//
85
86PHINode::PHINode(const PHINode &PN)
87 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000088 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000089 ReservedSpace(PN.getNumOperands()) {
90 Use *OL = OperandList;
91 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000092 OL[i] = PN.getOperand(i);
93 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000094 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000095 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000096}
97
Gordon Henriksen14a55692007-12-10 02:14:30 +000098PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +000099 if (OperandList)
100 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101}
102
103// removeIncomingValue - Remove an incoming value. This is useful if a
104// predecessor basic block is deleted.
105Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
106 unsigned NumOps = getNumOperands();
107 Use *OL = OperandList;
108 assert(Idx*2 < NumOps && "BB not in PHI node!");
109 Value *Removed = OL[Idx*2];
110
111 // Move everything after this operand down.
112 //
113 // FIXME: we could just swap with the end of the list, then erase. However,
114 // client might not expect this to happen. The code as it is thrashes the
115 // use/def lists, which is kinda lame.
116 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
117 OL[i-2] = OL[i];
118 OL[i-2+1] = OL[i+1];
119 }
120
121 // Nuke the last value.
122 OL[NumOps-2].set(0);
123 OL[NumOps-2+1].set(0);
124 NumOperands = NumOps-2;
125
126 // If the PHI node is dead, because it has zero entries, nuke it now.
127 if (NumOps == 2 && DeletePHIIfEmpty) {
128 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000129 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130 eraseFromParent();
131 }
132 return Removed;
133}
134
135/// resizeOperands - resize operands - This adjusts the length of the operands
136/// list according to the following behavior:
137/// 1. If NumOps == 0, grow the operand list in response to a push_back style
138/// of operation. This grows the number of ops by 1.5 times.
139/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
140/// 3. If NumOps == NumOperands, trim the reserved space.
141///
142void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000143 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000145 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000146 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
147 } else if (NumOps*2 > NumOperands) {
148 // No resize needed.
149 if (ReservedSpace >= NumOps) return;
150 } else if (NumOps == NumOperands) {
151 if (ReservedSpace == NumOps) return;
152 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000154 }
155
156 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000158 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000159 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000161 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
Nate Begemanb3923212005-08-04 23:24:19 +0000164/// hasConstantValue - If the specified PHI node always merges together the same
165/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000166Value *PHINode::hasConstantValue() const {
167 // Exploit the fact that phi nodes always have at least one entry.
168 Value *ConstantValue = getIncomingValue(0);
169 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
170 if (getIncomingValue(i) != ConstantValue)
171 return 0; // Incoming values not all the same.
172 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000173}
174
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000175
176//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000177// CallInst Implementation
178//===----------------------------------------------------------------------===//
179
Gordon Henriksen14a55692007-12-10 02:14:30 +0000180CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181}
182
Chris Lattner054ba2c2007-02-13 00:58:44 +0000183void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000184 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000185 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000186
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000188 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000189 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000190
Chris Lattner054ba2c2007-02-13 00:58:44 +0000191 assert((NumParams == FTy->getNumParams() ||
192 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000193 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000194 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000195 assert((i >= FTy->getNumParams() ||
196 FTy->getParamType(i) == Params[i]->getType()) &&
197 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000198 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000199 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000200}
201
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000203 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000204 Op<-1>() = Func;
205 Op<0>() = Actual1;
206 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000207
208 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000209 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000210 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000211
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000213 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000214 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000215 assert((0 >= FTy->getNumParams() ||
216 FTy->getParamType(0) == Actual1->getType()) &&
217 "Calling a function with a bad signature!");
218 assert((1 >= FTy->getNumParams() ||
219 FTy->getParamType(1) == Actual2->getType()) &&
220 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000221}
222
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000224 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000225 Op<-1>() = Func;
226 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000227
228 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000230 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000231
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232 assert((FTy->getNumParams() == 1 ||
233 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000235 assert((0 == FTy->getNumParams() ||
236 FTy->getParamType(0) == Actual->getType()) &&
237 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000238}
239
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000241 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000242 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000243
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000244 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000245 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000246 (void)FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000248 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000249}
250
Daniel Dunbar4975db62009-07-25 04:41:11 +0000251CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000252 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
254 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000255 Instruction::Call,
256 OperandTraits<CallInst>::op_end(this) - 2,
257 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000259 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260}
261
Daniel Dunbar4975db62009-07-25 04:41:11 +0000262CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263 BasicBlock *InsertAtEnd)
264 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
265 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000266 Instruction::Call,
267 OperandTraits<CallInst>::op_end(this) - 2,
268 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000270 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000272CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 Instruction *InsertBefore)
274 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
275 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000276 Instruction::Call,
277 OperandTraits<CallInst>::op_end(this) - 1,
278 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000280 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281}
282
Daniel Dunbar4975db62009-07-25 04:41:11 +0000283CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 BasicBlock *InsertAtEnd)
285 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
286 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000287 Instruction::Call,
288 OperandTraits<CallInst>::op_end(this) - 1,
289 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000291 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292}
293
Misha Brukmanb1c93172005-04-21 23:48:37 +0000294CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000295 : Instruction(CI.getType(), Instruction::Call,
296 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000297 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000298 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000299 setTailCall(CI.isTailCall());
300 setCallingConv(CI.getCallingConv());
301
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000302 Use *OL = OperandList;
303 Use *InOL = CI.OperandList;
304 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000305 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000306 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307}
308
Devang Patel4c758ea2008-09-25 21:00:45 +0000309void CallInst::addAttribute(unsigned i, Attributes attr) {
310 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000311 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000312 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000313}
314
Devang Patel4c758ea2008-09-25 21:00:45 +0000315void CallInst::removeAttribute(unsigned i, Attributes attr) {
316 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000317 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000318 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000319}
320
Devang Patelba3fa6c2008-09-23 23:03:40 +0000321bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000322 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000323 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000324 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000325 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000326 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000327}
328
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000329/// IsConstantOne - Return true only if val is constant int 1
330static bool IsConstantOne(Value *val) {
331 assert(val && "IsConstantOne does not work with NULL val");
332 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
333}
334
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000335static Instruction *createMalloc(Instruction *InsertBefore,
336 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000337 const Type *AllocTy, Value *AllocSize,
338 Value *ArraySize, Function *MallocF,
339 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000340 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000341 "createMalloc needs either InsertBefore or InsertAtEnd");
342
343 // malloc(type) becomes:
344 // bitcast (i8* malloc(typeSize)) to type*
345 // malloc(type, arraySize) becomes:
346 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000347 if (!ArraySize)
348 ArraySize = ConstantInt::get(IntPtrTy, 1);
349 else if (ArraySize->getType() != IntPtrTy) {
350 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000351 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
352 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000353 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000354 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
355 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000356 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000357
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000358 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000359 if (IsConstantOne(AllocSize)) {
360 AllocSize = ArraySize; // Operand * 1 = Operand
361 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
362 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
363 false /*ZExt*/);
364 // Malloc arg is constant product of type size and array size
365 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
366 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000367 // Multiply type size by the array size...
368 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000369 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
370 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000371 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000372 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
373 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000374 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000375 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000376
Victor Hernandez788eaab2009-09-18 19:20:02 +0000377 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378 // Create the call to Malloc.
379 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
380 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000381 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000382 Value *MallocFunc = MallocF;
383 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000384 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000385 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000386 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000387 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000388 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000389 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000390 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000391 Result = MCall;
392 if (Result->getType() != AllocPtrType)
393 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000394 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000395 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000396 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000397 Result = MCall;
398 if (Result->getType() != AllocPtrType) {
399 InsertAtEnd->getInstList().push_back(MCall);
400 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000401 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000402 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000403 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000404 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000405 if (Function *F = dyn_cast<Function>(MallocFunc)) {
406 MCall->setCallingConv(F->getCallingConv());
407 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
408 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000409 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000410
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000411 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000412}
413
414/// CreateMalloc - Generate the IR for a call to malloc:
415/// 1. Compute the malloc call's argument as the specified type's size,
416/// possibly multiplied by the array size if the array size is not
417/// constant 1.
418/// 2. Call malloc with that argument.
419/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000420Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
421 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000422 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000423 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000424 const Twine &Name) {
425 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000426 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427}
428
429/// CreateMalloc - Generate the IR for a call to malloc:
430/// 1. Compute the malloc call's argument as the specified type's size,
431/// possibly multiplied by the array size if the array size is not
432/// constant 1.
433/// 2. Call malloc with that argument.
434/// 3. Bitcast the result of the malloc call to the specified type.
435/// Note: This function does not add the bitcast to the basic block, that is the
436/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000437Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
438 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000439 Value *AllocSize, Value *ArraySize,
440 Function *MallocF, const Twine &Name) {
441 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000442 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000443}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000444
Victor Hernandeze2971492009-10-24 04:23:03 +0000445static Instruction* createFree(Value* Source, Instruction *InsertBefore,
446 BasicBlock *InsertAtEnd) {
447 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
448 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000449 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000450 "Can not free something of nonpointer type!");
451
452 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
453 Module* M = BB->getParent()->getParent();
454
455 const Type *VoidTy = Type::getVoidTy(M->getContext());
456 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
457 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000458 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000459 CallInst* Result = NULL;
460 Value *PtrCast = Source;
461 if (InsertBefore) {
462 if (Source->getType() != IntPtrTy)
463 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
464 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
465 } else {
466 if (Source->getType() != IntPtrTy)
467 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
468 Result = CallInst::Create(FreeFunc, PtrCast, "");
469 }
470 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000471 if (Function *F = dyn_cast<Function>(FreeFunc))
472 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000473
474 return Result;
475}
476
477/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000478Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
479 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000480}
481
482/// CreateFree - Generate the IR for a call to the builtin free function.
483/// Note: This function does not add the call to the basic block, that is the
484/// responsibility of the caller.
485Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
486 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
487 assert(FreeCall && "CreateFree did not create a CallInst");
488 return FreeCall;
489}
490
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000491//===----------------------------------------------------------------------===//
492// InvokeInst Implementation
493//===----------------------------------------------------------------------===//
494
495void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000496 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000497 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000498 Op<-3>() = Fn;
499 Op<-2>() = IfNormal;
500 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000501 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000502 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000503 (void)FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000504
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000505 assert(((NumArgs == FTy->getNumParams()) ||
506 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000507 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000508
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000509 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000510 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000511 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000512 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000513 "Invoking a function with a bad signature!");
514
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000515 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000516 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000517}
518
Misha Brukmanb1c93172005-04-21 23:48:37 +0000519InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000520 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000521 OperandTraits<InvokeInst>::op_end(this)
522 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000523 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000524 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000525 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000526 Use *OL = OperandList, *InOL = II.OperandList;
527 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000528 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000529 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000530}
531
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
533 return getSuccessor(idx);
534}
535unsigned InvokeInst::getNumSuccessorsV() const {
536 return getNumSuccessors();
537}
538void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
539 return setSuccessor(idx, B);
540}
541
Devang Patelba3fa6c2008-09-23 23:03:40 +0000542bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000543 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000544 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000545 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000546 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000547 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000548}
549
Devang Patel4c758ea2008-09-25 21:00:45 +0000550void InvokeInst::addAttribute(unsigned i, Attributes attr) {
551 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000552 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000553 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000554}
555
Devang Patel4c758ea2008-09-25 21:00:45 +0000556void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
557 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000558 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000559 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000560}
561
Duncan Sands5208d1a2007-11-28 17:07:01 +0000562
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000563//===----------------------------------------------------------------------===//
564// ReturnInst Implementation
565//===----------------------------------------------------------------------===//
566
Chris Lattner2195fc42007-02-24 00:55:48 +0000567ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000568 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000569 OperandTraits<ReturnInst>::op_end(this) -
570 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000571 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000572 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000573 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000574 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000575}
576
Owen Anderson55f1c092009-08-13 21:58:54 +0000577ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
578 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000579 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
580 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000581 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000582 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000583}
Owen Anderson55f1c092009-08-13 21:58:54 +0000584ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
585 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000586 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
587 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000588 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000589 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000590}
Owen Anderson55f1c092009-08-13 21:58:54 +0000591ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
592 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000593 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000594}
595
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596unsigned ReturnInst::getNumSuccessorsV() const {
597 return getNumSuccessors();
598}
599
Devang Patelae682fb2008-02-26 17:56:20 +0000600/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
601/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000603 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000604}
605
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000607 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608 return 0;
609}
610
Devang Patel59643e52008-02-23 00:35:18 +0000611ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000612}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000614//===----------------------------------------------------------------------===//
615// UnwindInst Implementation
616//===----------------------------------------------------------------------===//
617
Owen Anderson55f1c092009-08-13 21:58:54 +0000618UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
619 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
620 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000621}
Owen Anderson55f1c092009-08-13 21:58:54 +0000622UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
623 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
624 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000625}
626
627
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628unsigned UnwindInst::getNumSuccessorsV() const {
629 return getNumSuccessors();
630}
631
632void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000633 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000634}
635
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000637 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638 return 0;
639}
640
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000641//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000642// UnreachableInst Implementation
643//===----------------------------------------------------------------------===//
644
Owen Anderson55f1c092009-08-13 21:58:54 +0000645UnreachableInst::UnreachableInst(LLVMContext &Context,
646 Instruction *InsertBefore)
647 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
648 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000649}
Owen Anderson55f1c092009-08-13 21:58:54 +0000650UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
651 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
652 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000653}
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655unsigned UnreachableInst::getNumSuccessorsV() const {
656 return getNumSuccessors();
657}
658
659void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000660 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661}
662
663BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000664 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000666}
667
668//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000669// BranchInst Implementation
670//===----------------------------------------------------------------------===//
671
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672void BranchInst::AssertOK() {
673 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000674 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000675 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000676}
677
Chris Lattner2195fc42007-02-24 00:55:48 +0000678BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000679 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000680 OperandTraits<BranchInst>::op_end(this) - 1,
681 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000682 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000683 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000684}
685BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
686 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000687 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000688 OperandTraits<BranchInst>::op_end(this) - 3,
689 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000690 Op<-1>() = IfTrue;
691 Op<-2>() = IfFalse;
692 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000693#ifndef NDEBUG
694 AssertOK();
695#endif
696}
697
698BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000699 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000700 OperandTraits<BranchInst>::op_end(this) - 1,
701 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000702 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000703 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
705
706BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
707 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000708 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000709 OperandTraits<BranchInst>::op_end(this) - 3,
710 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000711 Op<-1>() = IfTrue;
712 Op<-2>() = IfFalse;
713 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000714#ifndef NDEBUG
715 AssertOK();
716#endif
717}
718
719
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000721 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000722 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
723 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000724 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 if (BI.getNumOperands() != 1) {
726 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000727 Op<-3>() = BI.Op<-3>();
728 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000730 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731}
732
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000733
734Use* Use::getPrefix() {
735 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
736 if (PotentialPrefix.getOpaqueValue())
737 return 0;
738
739 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
740}
741
742BranchInst::~BranchInst() {
743 if (NumOperands == 1) {
744 if (Use *Prefix = OperandList->getPrefix()) {
745 Op<-1>() = 0;
746 //
747 // mark OperandList to have a special value for scrutiny
748 // by baseclass destructors and operator delete
749 OperandList = Prefix;
750 } else {
751 NumOperands = 3;
752 OperandList = op_begin();
753 }
754 }
755}
756
757
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
759 return getSuccessor(idx);
760}
761unsigned BranchInst::getNumSuccessorsV() const {
762 return getNumSuccessors();
763}
764void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
765 setSuccessor(idx, B);
766}
767
768
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000769//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000770// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000771//===----------------------------------------------------------------------===//
772
Owen Andersonb6b25302009-07-14 23:09:55 +0000773static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000775 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000776 else {
777 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000778 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000779 assert(Amt->getType()->isIntegerTy() &&
780 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000781 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000782 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000783}
784
Victor Hernandez8acf2952009-10-23 21:09:37 +0000785AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
786 const Twine &Name, Instruction *InsertBefore)
787 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
788 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
789 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000790 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000791 setName(Name);
792}
793
794AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
795 const Twine &Name, BasicBlock *InsertAtEnd)
796 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
797 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
798 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000799 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000800 setName(Name);
801}
802
803AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
804 Instruction *InsertBefore)
805 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
806 getAISize(Ty->getContext(), 0), InsertBefore) {
807 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000808 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000809 setName(Name);
810}
811
812AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
813 BasicBlock *InsertAtEnd)
814 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
815 getAISize(Ty->getContext(), 0), InsertAtEnd) {
816 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000817 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000818 setName(Name);
819}
820
821AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
822 const Twine &Name, Instruction *InsertBefore)
823 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000824 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000825 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000826 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000827 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828}
829
Victor Hernandez8acf2952009-10-23 21:09:37 +0000830AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
831 const Twine &Name, BasicBlock *InsertAtEnd)
832 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000833 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000834 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000835 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000836 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000837}
838
Gordon Henriksen14a55692007-12-10 02:14:30 +0000839// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000840AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000841}
842
Victor Hernandez8acf2952009-10-23 21:09:37 +0000843void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000844 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000845 assert(Align <= MaximumAlignment &&
846 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000847 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000848 assert(getAlignment() == Align && "Alignment representation error!");
849}
850
Victor Hernandez8acf2952009-10-23 21:09:37 +0000851bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000852 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000853 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000854 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000855}
856
Victor Hernandez8acf2952009-10-23 21:09:37 +0000857const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858 return getType()->getElementType();
859}
860
Chris Lattner8b291e62008-11-26 02:54:17 +0000861/// isStaticAlloca - Return true if this alloca is in the entry block of the
862/// function and is a constant size. If so, the code generator will fold it
863/// into the prolog/epilog code, so it is basically free.
864bool AllocaInst::isStaticAlloca() const {
865 // Must be constant size.
866 if (!isa<ConstantInt>(getArraySize())) return false;
867
868 // Must be in the entry block.
869 const BasicBlock *Parent = getParent();
870 return Parent == &Parent->getParent()->front();
871}
872
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874// LoadInst Implementation
875//===----------------------------------------------------------------------===//
876
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000877void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000878 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880}
881
Daniel Dunbar4975db62009-07-25 04:41:11 +0000882LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000883 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000884 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000885 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000886 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000888 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889}
890
Daniel Dunbar4975db62009-07-25 04:41:11 +0000891LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000893 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000894 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000895 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000896 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000897 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898}
899
Daniel Dunbar4975db62009-07-25 04:41:11 +0000900LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000902 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000903 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000904 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000905 setAlignment(0);
906 AssertOK();
907 setName(Name);
908}
909
Daniel Dunbar4975db62009-07-25 04:41:11 +0000910LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000911 unsigned Align, Instruction *InsertBef)
912 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
913 Load, Ptr, InsertBef) {
914 setVolatile(isVolatile);
915 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000917 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000918}
919
Daniel Dunbar4975db62009-07-25 04:41:11 +0000920LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000921 unsigned Align, BasicBlock *InsertAE)
922 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
923 Load, Ptr, InsertAE) {
924 setVolatile(isVolatile);
925 setAlignment(Align);
926 AssertOK();
927 setName(Name);
928}
929
Daniel Dunbar4975db62009-07-25 04:41:11 +0000930LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000931 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000933 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000934 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000935 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000936 AssertOK();
937 setName(Name);
938}
939
Daniel Dunbar27096822009-08-11 18:11:15 +0000940
941
942LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
943 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
944 Load, Ptr, InsertBef) {
945 setVolatile(false);
946 setAlignment(0);
947 AssertOK();
948 if (Name && Name[0]) setName(Name);
949}
950
951LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
952 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
953 Load, Ptr, InsertAE) {
954 setVolatile(false);
955 setAlignment(0);
956 AssertOK();
957 if (Name && Name[0]) setName(Name);
958}
959
960LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
961 Instruction *InsertBef)
962: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
963 Load, Ptr, InsertBef) {
964 setVolatile(isVolatile);
965 setAlignment(0);
966 AssertOK();
967 if (Name && Name[0]) setName(Name);
968}
969
970LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
971 BasicBlock *InsertAE)
972 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
973 Load, Ptr, InsertAE) {
974 setVolatile(isVolatile);
975 setAlignment(0);
976 AssertOK();
977 if (Name && Name[0]) setName(Name);
978}
979
Christopher Lamb84485702007-04-22 19:24:39 +0000980void LoadInst::setAlignment(unsigned Align) {
981 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000982 assert(Align <= MaximumAlignment &&
983 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000984 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
985 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +0000986 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +0000987}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988
989//===----------------------------------------------------------------------===//
990// StoreInst Implementation
991//===----------------------------------------------------------------------===//
992
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000994 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +0000995 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000996 "Ptr must have pointer type!");
997 assert(getOperand(0)->getType() ==
998 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000999 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001000}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001001
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002
1003StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001004 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001005 OperandTraits<StoreInst>::op_begin(this),
1006 OperandTraits<StoreInst>::operands(this),
1007 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001008 Op<0>() = val;
1009 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001010 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001011 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001012 AssertOK();
1013}
1014
1015StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001016 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001017 OperandTraits<StoreInst>::op_begin(this),
1018 OperandTraits<StoreInst>::operands(this),
1019 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001020 Op<0>() = val;
1021 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001022 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001023 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001024 AssertOK();
1025}
1026
Misha Brukmanb1c93172005-04-21 23:48:37 +00001027StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001028 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001029 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001030 OperandTraits<StoreInst>::op_begin(this),
1031 OperandTraits<StoreInst>::operands(this),
1032 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001033 Op<0>() = val;
1034 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001035 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001036 setAlignment(0);
1037 AssertOK();
1038}
1039
1040StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1041 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001042 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001043 OperandTraits<StoreInst>::op_begin(this),
1044 OperandTraits<StoreInst>::operands(this),
1045 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001046 Op<0>() = val;
1047 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001048 setVolatile(isVolatile);
1049 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001050 AssertOK();
1051}
1052
Misha Brukmanb1c93172005-04-21 23:48:37 +00001053StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001054 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001055 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001056 OperandTraits<StoreInst>::op_begin(this),
1057 OperandTraits<StoreInst>::operands(this),
1058 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001059 Op<0>() = val;
1060 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001061 setVolatile(isVolatile);
1062 setAlignment(Align);
1063 AssertOK();
1064}
1065
1066StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001067 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001068 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001069 OperandTraits<StoreInst>::op_begin(this),
1070 OperandTraits<StoreInst>::operands(this),
1071 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001072 Op<0>() = val;
1073 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001074 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001075 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001076 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001077}
1078
Christopher Lamb84485702007-04-22 19:24:39 +00001079void StoreInst::setAlignment(unsigned Align) {
1080 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001081 assert(Align <= MaximumAlignment &&
1082 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001083 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1084 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001085 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001086}
1087
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001088//===----------------------------------------------------------------------===//
1089// GetElementPtrInst Implementation
1090//===----------------------------------------------------------------------===//
1091
Christopher Lambedf07882007-12-17 01:12:55 +00001092static unsigned retrieveAddrSpace(const Value *Val) {
1093 return cast<PointerType>(Val->getType())->getAddressSpace();
1094}
1095
Gabor Greif21ba1842008-06-06 20:28:12 +00001096void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001097 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001098 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1099 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001100 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001101
Chris Lattner79807c3d2007-01-31 19:47:18 +00001102 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001103 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001104
1105 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001106}
1107
Daniel Dunbar4975db62009-07-25 04:41:11 +00001108void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001109 assert(NumOperands == 2 && "NumOperands not initialized?");
1110 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001111 OL[0] = Ptr;
1112 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001113
1114 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001115}
1116
Gabor Greiff6caff662008-05-10 08:32:32 +00001117GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001118 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001119 OperandTraits<GetElementPtrInst>::op_end(this)
1120 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001121 GEPI.getNumOperands()) {
1122 Use *OL = OperandList;
1123 Use *GEPIOL = GEPI.OperandList;
1124 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001125 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001126 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001127}
1128
Chris Lattner82981202005-05-03 05:43:30 +00001129GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001130 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001131 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001132 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001133 GetElementPtr,
1134 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1135 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001136 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001137}
1138
1139GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001140 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001141 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001142 checkType(getIndexedType(Ptr->getType(),Idx)),
1143 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001144 GetElementPtr,
1145 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1146 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001147 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001148}
1149
Chris Lattner6090a422009-03-09 04:46:40 +00001150/// getIndexedType - Returns the type of the element that would be accessed with
1151/// a gep instruction with the specified parameters.
1152///
1153/// The Idxs pointer should point to a continuous piece of memory containing the
1154/// indices, either as Value* or uint64_t.
1155///
1156/// A null type is returned if the indices are invalid for the specified
1157/// pointer type.
1158///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001159template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001160static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1161 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001162 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1163 if (!PTy) return 0; // Type isn't a pointer type!
1164 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001165
Chris Lattner6090a422009-03-09 04:46:40 +00001166 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001167 if (NumIdx == 0)
1168 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001169
1170 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001171 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1172 // that contain opaque types) under the assumption that it will be resolved to
1173 // a sane type later.
1174 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001175 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001176
Dan Gohman1ecaf452008-05-31 00:58:22 +00001177 unsigned CurIdx = 1;
1178 for (; CurIdx != NumIdx; ++CurIdx) {
1179 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001180 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001181 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001182 if (!CT->indexValid(Index)) return 0;
1183 Agg = CT->getTypeAtIndex(Index);
1184
1185 // If the new type forwards to another type, then it is in the middle
1186 // of being refined to another type (and hence, may have dropped all
1187 // references to what it was using before). So, use the new forwarded
1188 // type.
1189 if (const Type *Ty = Agg->getForwardedType())
1190 Agg = Ty;
1191 }
1192 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193}
1194
Matthijs Kooijman04468622008-07-29 08:46:11 +00001195const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1196 Value* const *Idxs,
1197 unsigned NumIdx) {
1198 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1199}
1200
1201const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1202 uint64_t const *Idxs,
1203 unsigned NumIdx) {
1204 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1205}
1206
Chris Lattner82981202005-05-03 05:43:30 +00001207const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1208 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1209 if (!PTy) return 0; // Type isn't a pointer type!
1210
1211 // Check the pointer index.
1212 if (!PTy->indexValid(Idx)) return 0;
1213
Chris Lattnerc2233332005-05-03 16:44:45 +00001214 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001215}
1216
Chris Lattner45f15572007-04-14 00:12:57 +00001217
1218/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1219/// zeros. If so, the result pointer and the first operand have the same
1220/// value, just potentially different types.
1221bool GetElementPtrInst::hasAllZeroIndices() const {
1222 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1223 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1224 if (!CI->isZero()) return false;
1225 } else {
1226 return false;
1227 }
1228 }
1229 return true;
1230}
1231
Chris Lattner27058292007-04-27 20:35:56 +00001232/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1233/// constant integers. If so, the result pointer and the first operand have
1234/// a constant offset between them.
1235bool GetElementPtrInst::hasAllConstantIndices() const {
1236 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1237 if (!isa<ConstantInt>(getOperand(i)))
1238 return false;
1239 }
1240 return true;
1241}
1242
Dan Gohman1b849082009-09-07 23:54:19 +00001243void GetElementPtrInst::setIsInBounds(bool B) {
1244 cast<GEPOperator>(this)->setIsInBounds(B);
1245}
Chris Lattner45f15572007-04-14 00:12:57 +00001246
Nick Lewycky28a5f252009-09-27 21:33:04 +00001247bool GetElementPtrInst::isInBounds() const {
1248 return cast<GEPOperator>(this)->isInBounds();
1249}
1250
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001251//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001252// ExtractElementInst Implementation
1253//===----------------------------------------------------------------------===//
1254
1255ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001256 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001257 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001258 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001259 ExtractElement,
1260 OperandTraits<ExtractElementInst>::op_begin(this),
1261 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001262 assert(isValidOperands(Val, Index) &&
1263 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001264 Op<0>() = Val;
1265 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001266 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001267}
1268
1269ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001270 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001271 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001272 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001273 ExtractElement,
1274 OperandTraits<ExtractElementInst>::op_begin(this),
1275 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001276 assert(isValidOperands(Val, Index) &&
1277 "Invalid extractelement instruction operands!");
1278
Gabor Greif2d3024d2008-05-26 21:33:52 +00001279 Op<0>() = Val;
1280 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001281 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001282}
1283
Chris Lattner65511ff2006-10-05 06:24:58 +00001284
Chris Lattner54865b32006-04-08 04:05:48 +00001285bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001286 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001287 return false;
1288 return true;
1289}
1290
1291
Robert Bocchino23004482006-01-10 19:05:34 +00001292//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001293// InsertElementInst Implementation
1294//===----------------------------------------------------------------------===//
1295
Chris Lattner54865b32006-04-08 04:05:48 +00001296InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001297 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001298 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001299 : Instruction(Vec->getType(), InsertElement,
1300 OperandTraits<InsertElementInst>::op_begin(this),
1301 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001302 assert(isValidOperands(Vec, Elt, Index) &&
1303 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001304 Op<0>() = Vec;
1305 Op<1>() = Elt;
1306 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001307 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001308}
1309
Chris Lattner54865b32006-04-08 04:05:48 +00001310InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001311 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001312 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001313 : Instruction(Vec->getType(), InsertElement,
1314 OperandTraits<InsertElementInst>::op_begin(this),
1315 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001316 assert(isValidOperands(Vec, Elt, Index) &&
1317 "Invalid insertelement instruction operands!");
1318
Gabor Greif2d3024d2008-05-26 21:33:52 +00001319 Op<0>() = Vec;
1320 Op<1>() = Elt;
1321 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001322 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001323}
1324
Chris Lattner54865b32006-04-08 04:05:48 +00001325bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1326 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001327 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001328 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001329
Reid Spencerd84d35b2007-02-15 02:26:10 +00001330 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001331 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001332
Duncan Sands9dff9be2010-02-15 16:12:20 +00001333 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001334 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001335 return true;
1336}
1337
1338
Robert Bocchinoca27f032006-01-17 20:07:22 +00001339//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001340// ShuffleVectorInst Implementation
1341//===----------------------------------------------------------------------===//
1342
1343ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001344 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001345 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001346: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001347 cast<VectorType>(Mask->getType())->getNumElements()),
1348 ShuffleVector,
1349 OperandTraits<ShuffleVectorInst>::op_begin(this),
1350 OperandTraits<ShuffleVectorInst>::operands(this),
1351 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001352 assert(isValidOperands(V1, V2, Mask) &&
1353 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001354 Op<0>() = V1;
1355 Op<1>() = V2;
1356 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001357 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001358}
1359
1360ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001361 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001362 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001363: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1364 cast<VectorType>(Mask->getType())->getNumElements()),
1365 ShuffleVector,
1366 OperandTraits<ShuffleVectorInst>::op_begin(this),
1367 OperandTraits<ShuffleVectorInst>::operands(this),
1368 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001369 assert(isValidOperands(V1, V2, Mask) &&
1370 "Invalid shuffle vector instruction operands!");
1371
Gabor Greif2d3024d2008-05-26 21:33:52 +00001372 Op<0>() = V1;
1373 Op<1>() = V2;
1374 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001375 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001376}
1377
Mon P Wang25f01062008-11-10 04:46:22 +00001378bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001379 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001380 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001381 return false;
1382
1383 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001384 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001385 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001386
1387 // Check to see if Mask is valid.
1388 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1389 const VectorType *VTy = cast<VectorType>(V1->getType());
1390 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1391 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1392 if (CI->uge(VTy->getNumElements()*2))
1393 return false;
1394 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1395 return false;
1396 }
1397 }
1398 }
1399 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1400 return false;
1401
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001402 return true;
1403}
1404
Chris Lattnerf724e342008-03-02 05:28:33 +00001405/// getMaskValue - Return the index from the shuffle mask for the specified
1406/// output result. This is either -1 if the element is undef or a number less
1407/// than 2*numelements.
1408int ShuffleVectorInst::getMaskValue(unsigned i) const {
1409 const Constant *Mask = cast<Constant>(getOperand(2));
1410 if (isa<UndefValue>(Mask)) return -1;
1411 if (isa<ConstantAggregateZero>(Mask)) return 0;
1412 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1413 assert(i < MaskCV->getNumOperands() && "Index out of range");
1414
1415 if (isa<UndefValue>(MaskCV->getOperand(i)))
1416 return -1;
1417 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1418}
1419
Dan Gohman12fce772008-05-15 19:50:34 +00001420//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001421// InsertValueInst Class
1422//===----------------------------------------------------------------------===//
1423
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001424void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001425 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001426 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001427 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx, Idx + NumIdx) ==
1428 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001429 Op<0>() = Agg;
1430 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001431
Dan Gohmandd41bba2010-06-21 19:47:52 +00001432 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001433 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001434}
1435
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001436void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001437 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001438 assert(NumOperands == 2 && "NumOperands not initialized?");
Frits van Bommel16ebe772010-12-05 20:50:26 +00001439 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idx) == Val->getType()
1440 && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001441 Op<0>() = Agg;
1442 Op<1>() = Val;
1443
1444 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001445 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001446}
1447
1448InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001449 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001450 OperandTraits<InsertValueInst>::op_begin(this), 2),
1451 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001452 Op<0>() = IVI.getOperand(0);
1453 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001454 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001455}
1456
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001457InsertValueInst::InsertValueInst(Value *Agg,
1458 Value *Val,
1459 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001460 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001461 Instruction *InsertBefore)
1462 : Instruction(Agg->getType(), InsertValue,
1463 OperandTraits<InsertValueInst>::op_begin(this),
1464 2, InsertBefore) {
1465 init(Agg, Val, Idx, Name);
1466}
1467
1468InsertValueInst::InsertValueInst(Value *Agg,
1469 Value *Val,
1470 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001471 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001472 BasicBlock *InsertAtEnd)
1473 : Instruction(Agg->getType(), InsertValue,
1474 OperandTraits<InsertValueInst>::op_begin(this),
1475 2, InsertAtEnd) {
1476 init(Agg, Val, Idx, Name);
1477}
1478
Dan Gohman0752bff2008-05-23 00:36:11 +00001479//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001480// ExtractValueInst Class
1481//===----------------------------------------------------------------------===//
1482
Gabor Greifcbcc4952008-06-06 21:06:32 +00001483void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001484 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001485 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001486
Dan Gohmandd41bba2010-06-21 19:47:52 +00001487 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001488 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001489}
1490
Daniel Dunbar4975db62009-07-25 04:41:11 +00001491void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001492 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001493
1494 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001495 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001496}
1497
1498ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001499 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001500 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001501 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001502}
1503
Dan Gohman12fce772008-05-15 19:50:34 +00001504// getIndexedType - Returns the type of the element that would be extracted
1505// with an extractvalue instruction with the specified parameters.
1506//
1507// A null type is returned if the indices are invalid for the specified
1508// pointer type.
1509//
1510const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001511 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001512 unsigned NumIdx) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001513 for (unsigned CurIdx = 0; CurIdx != NumIdx; ++CurIdx) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001514 unsigned Index = Idxs[CurIdx];
Frits van Bommel16ebe772010-12-05 20:50:26 +00001515 // We can't use CompositeType::indexValid(Index) here.
1516 // indexValid() always returns true for arrays because getelementptr allows
1517 // out-of-bounds indices. Since we don't allow those for extractvalue and
1518 // insertvalue we need to check array indexing manually.
1519 // Since the only other types we can index into are struct types it's just
1520 // as easy to check those manually as well.
1521 if (const ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
1522 if (Index >= AT->getNumElements())
1523 return 0;
1524 } else if (const StructType *ST = dyn_cast<StructType>(Agg)) {
1525 if (Index >= ST->getNumElements())
1526 return 0;
1527 } else {
1528 // Not a valid type to index into.
1529 return 0;
1530 }
1531
1532 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001533
1534 // If the new type forwards to another type, then it is in the middle
1535 // of being refined to another type (and hence, may have dropped all
1536 // references to what it was using before). So, use the new forwarded
1537 // type.
1538 if (const Type *Ty = Agg->getForwardedType())
1539 Agg = Ty;
1540 }
Frits van Bommel16ebe772010-12-05 20:50:26 +00001541 return Agg;
Dan Gohman12fce772008-05-15 19:50:34 +00001542}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001543
Dan Gohman4a3125b2008-06-17 21:07:55 +00001544const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001545 unsigned Idx) {
1546 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001547}
1548
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001549//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001550// BinaryOperator Class
1551//===----------------------------------------------------------------------===//
1552
Chris Lattner2195fc42007-02-24 00:55:48 +00001553BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001554 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001555 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001556 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001557 OperandTraits<BinaryOperator>::op_begin(this),
1558 OperandTraits<BinaryOperator>::operands(this),
1559 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001560 Op<0>() = S1;
1561 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001562 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001563 setName(Name);
1564}
1565
1566BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001567 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001568 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001569 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001570 OperandTraits<BinaryOperator>::op_begin(this),
1571 OperandTraits<BinaryOperator>::operands(this),
1572 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001573 Op<0>() = S1;
1574 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001575 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001576 setName(Name);
1577}
1578
1579
1580void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001581 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001582 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001583 assert(LHS->getType() == RHS->getType() &&
1584 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001585#ifndef NDEBUG
1586 switch (iType) {
1587 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001588 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001589 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001590 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001591 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001592 "Tried to create an integer operation on a non-integer type!");
1593 break;
1594 case FAdd: case FSub:
1595 case FMul:
1596 assert(getType() == LHS->getType() &&
1597 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001598 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001599 "Tried to create a floating-point operation on a "
1600 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001601 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001602 case UDiv:
1603 case SDiv:
1604 assert(getType() == LHS->getType() &&
1605 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001606 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001607 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001608 "Incorrect operand type (not integer) for S/UDIV");
1609 break;
1610 case FDiv:
1611 assert(getType() == LHS->getType() &&
1612 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001613 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001614 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001615 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001616 case URem:
1617 case SRem:
1618 assert(getType() == LHS->getType() &&
1619 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001620 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001621 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001622 "Incorrect operand type (not integer) for S/UREM");
1623 break;
1624 case FRem:
1625 assert(getType() == LHS->getType() &&
1626 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001627 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001628 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001629 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001630 case Shl:
1631 case LShr:
1632 case AShr:
1633 assert(getType() == LHS->getType() &&
1634 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001635 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001636 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001637 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001638 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001639 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001640 case And: case Or:
1641 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001642 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001643 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001644 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001645 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001646 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001647 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001648 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001649 default:
1650 break;
1651 }
1652#endif
1653}
1654
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001655BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001656 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001657 Instruction *InsertBefore) {
1658 assert(S1->getType() == S2->getType() &&
1659 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001660 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001661}
1662
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001663BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001664 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001665 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001666 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001667 InsertAtEnd->getInstList().push_back(Res);
1668 return Res;
1669}
1670
Dan Gohman5476cfd2009-08-12 16:23:25 +00001671BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001672 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001673 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001674 return new BinaryOperator(Instruction::Sub,
1675 zero, Op,
1676 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001677}
1678
Dan Gohman5476cfd2009-08-12 16:23:25 +00001679BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001680 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001681 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001682 return new BinaryOperator(Instruction::Sub,
1683 zero, Op,
1684 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001685}
1686
Dan Gohman4ab44202009-12-18 02:58:50 +00001687BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1688 Instruction *InsertBefore) {
1689 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1690 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1691}
1692
1693BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1694 BasicBlock *InsertAtEnd) {
1695 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1696 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1697}
1698
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001699BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1700 Instruction *InsertBefore) {
1701 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1702 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1703}
1704
1705BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1706 BasicBlock *InsertAtEnd) {
1707 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1708 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1709}
1710
Dan Gohman5476cfd2009-08-12 16:23:25 +00001711BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001712 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001713 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001714 return new BinaryOperator(Instruction::FSub,
1715 zero, Op,
1716 Op->getType(), Name, InsertBefore);
1717}
1718
Dan Gohman5476cfd2009-08-12 16:23:25 +00001719BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001720 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001721 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001722 return new BinaryOperator(Instruction::FSub,
1723 zero, Op,
1724 Op->getType(), Name, InsertAtEnd);
1725}
1726
Dan Gohman5476cfd2009-08-12 16:23:25 +00001727BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001728 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001729 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001730 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001731 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001732 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001733 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001734 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001735 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001736 }
1737
1738 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001739 Op->getType(), Name, InsertBefore);
1740}
1741
Dan Gohman5476cfd2009-08-12 16:23:25 +00001742BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001743 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001744 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001745 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001746 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001747 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001748 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001749 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001750 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001751 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001752 }
1753
1754 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001755 Op->getType(), Name, InsertAtEnd);
1756}
1757
1758
1759// isConstantAllOnes - Helper function for several functions below
1760static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001761 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1762 return CI->isAllOnesValue();
1763 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1764 return CV->isAllOnesValue();
1765 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001766}
1767
Owen Andersonbb2501b2009-07-13 22:18:28 +00001768bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001769 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1770 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001771 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1772 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001773 return false;
1774}
1775
Owen Andersonbb2501b2009-07-13 22:18:28 +00001776bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001777 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1778 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001779 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001780 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001781 return false;
1782}
1783
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001784bool BinaryOperator::isNot(const Value *V) {
1785 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1786 return (Bop->getOpcode() == Instruction::Xor &&
1787 (isConstantAllOnes(Bop->getOperand(1)) ||
1788 isConstantAllOnes(Bop->getOperand(0))));
1789 return false;
1790}
1791
Chris Lattner2c7d1772005-04-24 07:28:37 +00001792Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001793 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001794}
1795
Chris Lattner2c7d1772005-04-24 07:28:37 +00001796const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1797 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001798}
1799
Dan Gohmana5b96452009-06-04 22:49:04 +00001800Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001801 return cast<BinaryOperator>(BinOp)->getOperand(1);
1802}
1803
1804const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1805 return getFNegArgument(const_cast<Value*>(BinOp));
1806}
1807
Chris Lattner2c7d1772005-04-24 07:28:37 +00001808Value *BinaryOperator::getNotArgument(Value *BinOp) {
1809 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1810 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1811 Value *Op0 = BO->getOperand(0);
1812 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001813 if (isConstantAllOnes(Op0)) return Op1;
1814
1815 assert(isConstantAllOnes(Op1));
1816 return Op0;
1817}
1818
Chris Lattner2c7d1772005-04-24 07:28:37 +00001819const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1820 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001821}
1822
1823
1824// swapOperands - Exchange the two operands to this instruction. This
1825// instruction is safe to use on any binary instruction and does not
1826// modify the semantics of the instruction. If the instruction is
1827// order dependent (SetLT f.e.) the opcode is changed.
1828//
1829bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001830 if (!isCommutative())
1831 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001832 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833 return false;
1834}
1835
Dan Gohman1b849082009-09-07 23:54:19 +00001836void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1837 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1838}
1839
1840void BinaryOperator::setHasNoSignedWrap(bool b) {
1841 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1842}
1843
1844void BinaryOperator::setIsExact(bool b) {
1845 cast<SDivOperator>(this)->setIsExact(b);
1846}
1847
Nick Lewycky28a5f252009-09-27 21:33:04 +00001848bool BinaryOperator::hasNoUnsignedWrap() const {
1849 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1850}
1851
1852bool BinaryOperator::hasNoSignedWrap() const {
1853 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1854}
1855
1856bool BinaryOperator::isExact() const {
1857 return cast<SDivOperator>(this)->isExact();
1858}
1859
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001860//===----------------------------------------------------------------------===//
1861// CastInst Class
1862//===----------------------------------------------------------------------===//
1863
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001864// Just determine if this cast only deals with integral->integral conversion.
1865bool CastInst::isIntegerCast() const {
1866 switch (getOpcode()) {
1867 default: return false;
1868 case Instruction::ZExt:
1869 case Instruction::SExt:
1870 case Instruction::Trunc:
1871 return true;
1872 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001873 return getOperand(0)->getType()->isIntegerTy() &&
1874 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001876}
1877
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001878bool CastInst::isLosslessCast() const {
1879 // Only BitCast can be lossless, exit fast if we're not BitCast
1880 if (getOpcode() != Instruction::BitCast)
1881 return false;
1882
1883 // Identity cast is always lossless
1884 const Type* SrcTy = getOperand(0)->getType();
1885 const Type* DstTy = getType();
1886 if (SrcTy == DstTy)
1887 return true;
1888
Reid Spencer8d9336d2006-12-31 05:26:44 +00001889 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001890 if (SrcTy->isPointerTy())
1891 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001892 return false; // Other types have no identity values
1893}
1894
1895/// This function determines if the CastInst does not require any bits to be
1896/// changed in order to effect the cast. Essentially, it identifies cases where
1897/// no code gen is necessary for the cast, hence the name no-op cast. For
1898/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001899/// # bitcast i32* %x to i8*
1900/// # bitcast <2 x i32> %x to <4 x i16>
1901/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001902/// @brief Determine if the described cast is a no-op.
1903bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1904 const Type *SrcTy,
1905 const Type *DestTy,
1906 const Type *IntPtrTy) {
1907 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001908 default:
1909 assert(!"Invalid CastOp");
1910 case Instruction::Trunc:
1911 case Instruction::ZExt:
1912 case Instruction::SExt:
1913 case Instruction::FPTrunc:
1914 case Instruction::FPExt:
1915 case Instruction::UIToFP:
1916 case Instruction::SIToFP:
1917 case Instruction::FPToUI:
1918 case Instruction::FPToSI:
1919 return false; // These always modify bits
1920 case Instruction::BitCast:
1921 return true; // BitCast never modifies bits.
1922 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001923 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001924 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001925 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001926 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001927 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001928 }
1929}
1930
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001931/// @brief Determine if a cast is a no-op.
1932bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1933 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1934}
1935
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936/// This function determines if a pair of casts can be eliminated and what
1937/// opcode should be used in the elimination. This assumes that there are two
1938/// instructions like this:
1939/// * %F = firstOpcode SrcTy %x to MidTy
1940/// * %S = secondOpcode MidTy %F to DstTy
1941/// The function returns a resultOpcode so these two casts can be replaced with:
1942/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1943/// If no such cast is permited, the function returns 0.
1944unsigned CastInst::isEliminableCastPair(
1945 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1946 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1947{
1948 // Define the 144 possibilities for these two cast instructions. The values
1949 // in this matrix determine what to do in a given situation and select the
1950 // case in the switch below. The rows correspond to firstOp, the columns
1951 // correspond to secondOp. In looking at the table below, keep in mind
1952 // the following cast properties:
1953 //
1954 // Size Compare Source Destination
1955 // Operator Src ? Size Type Sign Type Sign
1956 // -------- ------------ ------------------- ---------------------
1957 // TRUNC > Integer Any Integral Any
1958 // ZEXT < Integral Unsigned Integer Any
1959 // SEXT < Integral Signed Integer Any
1960 // FPTOUI n/a FloatPt n/a Integral Unsigned
1961 // FPTOSI n/a FloatPt n/a Integral Signed
1962 // UITOFP n/a Integral Unsigned FloatPt n/a
1963 // SITOFP n/a Integral Signed FloatPt n/a
1964 // FPTRUNC > FloatPt n/a FloatPt n/a
1965 // FPEXT < FloatPt n/a FloatPt n/a
1966 // PTRTOINT n/a Pointer n/a Integral Unsigned
1967 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001968 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001969 //
1970 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001971 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1972 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001973 // of the produced value (we no longer know the top-part is all zeros).
1974 // Further this conversion is often much more expensive for typical hardware,
1975 // and causes issues when building libgcc. We disallow fptosi+sext for the
1976 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001977 const unsigned numCastOps =
1978 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1979 static const uint8_t CastResults[numCastOps][numCastOps] = {
1980 // T F F U S F F P I B -+
1981 // R Z S P P I I T P 2 N T |
1982 // U E E 2 2 2 2 R E I T C +- secondOp
1983 // N X X U S F F N X N 2 V |
1984 // C T T I I P P C T T P T -+
1985 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1986 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1987 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001988 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1989 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001990 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1991 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1992 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1993 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1994 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1995 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1996 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1997 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00001998
1999 // If either of the casts are a bitcast from scalar to vector, disallow the
2000 // merging.
2001 if ((firstOp == Instruction::BitCast &&
2002 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2003 (secondOp == Instruction::BitCast &&
2004 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2005 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002006
2007 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2008 [secondOp-Instruction::CastOpsBegin];
2009 switch (ElimCase) {
2010 case 0:
2011 // categorically disallowed
2012 return 0;
2013 case 1:
2014 // allowed, use first cast's opcode
2015 return firstOp;
2016 case 2:
2017 // allowed, use second cast's opcode
2018 return secondOp;
2019 case 3:
2020 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002021 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002022 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002023 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002024 return firstOp;
2025 return 0;
2026 case 4:
2027 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002028 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002029 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030 return firstOp;
2031 return 0;
2032 case 5:
2033 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002034 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002035 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002036 return secondOp;
2037 return 0;
2038 case 6:
2039 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002040 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002041 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002042 return secondOp;
2043 return 0;
2044 case 7: {
2045 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002046 if (!IntPtrTy)
2047 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002048 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2049 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050 if (MidSize >= PtrSize)
2051 return Instruction::BitCast;
2052 return 0;
2053 }
2054 case 8: {
2055 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2056 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2057 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002058 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2059 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 if (SrcSize == DstSize)
2061 return Instruction::BitCast;
2062 else if (SrcSize < DstSize)
2063 return firstOp;
2064 return secondOp;
2065 }
2066 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2067 return Instruction::ZExt;
2068 case 10:
2069 // fpext followed by ftrunc is allowed if the bit size returned to is
2070 // the same as the original, in which case its just a bitcast
2071 if (SrcTy == DstTy)
2072 return Instruction::BitCast;
2073 return 0; // If the types are not the same we can't eliminate it.
2074 case 11:
2075 // bitcast followed by ptrtoint is allowed as long as the bitcast
2076 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002077 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002078 return secondOp;
2079 return 0;
2080 case 12:
2081 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002082 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002083 return firstOp;
2084 return 0;
2085 case 13: {
2086 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002087 if (!IntPtrTy)
2088 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002089 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2090 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2091 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 if (SrcSize <= PtrSize && SrcSize == DstSize)
2093 return Instruction::BitCast;
2094 return 0;
2095 }
2096 case 99:
2097 // cast combination can't happen (error in input). This is for all cases
2098 // where the MidTy is not the same for the two cast instructions.
2099 assert(!"Invalid Cast Combination");
2100 return 0;
2101 default:
2102 assert(!"Error in CastResults table!!!");
2103 return 0;
2104 }
2105 return 0;
2106}
2107
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002108CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002109 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 // Construct and return the appropriate CastInst subclass
2111 switch (op) {
2112 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2113 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2114 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2115 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2116 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2117 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2118 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2119 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2120 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2121 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2122 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2123 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2124 default:
2125 assert(!"Invalid opcode provided");
2126 }
2127 return 0;
2128}
2129
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002130CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002131 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002132 // Construct and return the appropriate CastInst subclass
2133 switch (op) {
2134 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2135 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2136 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2137 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2138 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2139 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2140 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2141 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2142 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2143 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2144 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2145 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2146 default:
2147 assert(!"Invalid opcode provided");
2148 }
2149 return 0;
2150}
2151
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002152CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002153 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002154 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002155 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002156 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2157 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002158}
2159
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002160CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002161 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002162 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002163 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002164 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2165 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002166}
2167
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002168CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002169 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002170 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002171 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002172 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2173 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002174}
2175
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002177 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002178 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002179 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002180 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2181 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002182}
2183
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002184CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002185 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002186 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002187 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002188 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2189 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002190}
2191
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002192CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002193 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002194 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002195 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2197 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002198}
2199
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002200CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002201 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002202 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002203 assert(S->getType()->isPointerTy() && "Invalid cast");
2204 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002205 "Invalid cast");
2206
Duncan Sands9dff9be2010-02-15 16:12:20 +00002207 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002208 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2209 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002210}
2211
2212/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002213CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002214 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002215 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002216 assert(S->getType()->isPointerTy() && "Invalid cast");
2217 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002218 "Invalid cast");
2219
Duncan Sands9dff9be2010-02-15 16:12:20 +00002220 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002221 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2222 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002223}
2224
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002225CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002226 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002227 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002228 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002229 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002230 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2231 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002232 Instruction::CastOps opcode =
2233 (SrcBits == DstBits ? Instruction::BitCast :
2234 (SrcBits > DstBits ? Instruction::Trunc :
2235 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002236 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002237}
2238
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002239CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002240 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002241 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002242 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002243 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002244 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2245 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002246 Instruction::CastOps opcode =
2247 (SrcBits == DstBits ? Instruction::BitCast :
2248 (SrcBits > DstBits ? Instruction::Trunc :
2249 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002250 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002251}
2252
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002253CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002254 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002255 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002256 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002257 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002258 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2259 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002260 Instruction::CastOps opcode =
2261 (SrcBits == DstBits ? Instruction::BitCast :
2262 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002263 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002264}
2265
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002266CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002267 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002268 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002269 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002270 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002271 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2272 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002273 Instruction::CastOps opcode =
2274 (SrcBits == DstBits ? Instruction::BitCast :
2275 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002276 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002277}
2278
Duncan Sands55e50902008-01-06 10:12:28 +00002279// Check whether it is valid to call getCastOpcode for these types.
2280// This routine must be kept in sync with getCastOpcode.
2281bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2282 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2283 return false;
2284
2285 if (SrcTy == DestTy)
2286 return true;
2287
2288 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002289 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2290 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002291
2292 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002293 if (DestTy->isIntegerTy()) { // Casting to integral
2294 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002295 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002296 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002297 return true;
2298 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002299 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002300 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002301 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002302 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002303 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002304 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2305 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002306 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002307 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002308 return true;
2309 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002310 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002311 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002312 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002313 return false;
2314 }
2315 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002316 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002317 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002318 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002319 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002320 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002321 return DestPTy->getBitWidth() == SrcBits;
2322 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002323 } else if (DestTy->isPointerTy()) { // Casting to pointer
2324 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002325 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002326 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002327 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002328 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002329 return false;
2330 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002331 } else if (DestTy->isX86_MMXTy()) {
2332 return SrcBits == 64;
Gabor Greif697e94c2008-05-15 10:04:30 +00002333 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002334 return false;
2335 }
2336}
2337
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338// Provide a way to get a "cast" where the cast opcode is inferred from the
2339// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002340// logic in the castIsValid function below. This axiom should hold:
2341// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2342// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002344// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002346CastInst::getCastOpcode(
2347 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002348 // Get the bit sizes, we'll need these
2349 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002350 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2351 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352
Duncan Sands55e50902008-01-06 10:12:28 +00002353 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2354 "Only first class types are castable!");
2355
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002357 if (DestTy->isIntegerTy()) { // Casting to integral
2358 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359 if (DestBits < SrcBits)
2360 return Trunc; // int -> smaller int
2361 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002362 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 return SExt; // signed -> SEXT
2364 else
2365 return ZExt; // unsigned -> ZEXT
2366 } else {
2367 return BitCast; // Same size, No-op cast
2368 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002369 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002370 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002371 return FPToSI; // FP -> sint
2372 else
2373 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002374 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002376 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002377 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 return BitCast; // Same size, no-op cast
2379 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002380 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381 "Casting from a value that is not first-class type");
2382 return PtrToInt; // ptr -> int
2383 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002384 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2385 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002386 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387 return SIToFP; // sint -> FP
2388 else
2389 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002390 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 if (DestBits < SrcBits) {
2392 return FPTrunc; // FP -> smaller FP
2393 } else if (DestBits > SrcBits) {
2394 return FPExt; // FP -> larger FP
2395 } else {
2396 return BitCast; // same size, no-op cast
2397 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002398 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002400 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002401 PTy = NULL;
2402 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002404 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002406 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2407 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002409 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002410 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002411 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002413 return BitCast; // float/int -> vector
Dale Johannesendd224d22010-09-30 23:57:10 +00002414 } else if (SrcTy->isX86_MMXTy()) {
2415 assert(DestPTy->getBitWidth()==64 &&
2416 "Casting X86_MMX to vector of wrong width");
2417 return BitCast; // MMX to 64-bit vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002419 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002421 } else if (DestTy->isPointerTy()) {
2422 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002424 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 return IntToPtr; // int -> ptr
2426 } else {
2427 assert(!"Casting pointer to other than pointer or int");
2428 }
Dale Johannesendd224d22010-09-30 23:57:10 +00002429 } else if (DestTy->isX86_MMXTy()) {
Bill Wendling3c704172010-10-03 00:46:06 +00002430 if (isa<VectorType>(SrcTy)) {
2431 assert(cast<VectorType>(SrcTy)->getBitWidth() == 64 &&
Dale Johannesendd224d22010-09-30 23:57:10 +00002432 "Casting vector of wrong width to X86_MMX");
2433 return BitCast; // 64-bit vector to MMX
2434 } else {
2435 assert(!"Illegal cast to X86_MMX");
2436 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437 } else {
2438 assert(!"Casting to type that is not first-class");
2439 }
2440
2441 // If we fall through to here we probably hit an assertion cast above
2442 // and assertions are not turned on. Anything we return is an error, so
2443 // BitCast is as good a choice as any.
2444 return BitCast;
2445}
2446
2447//===----------------------------------------------------------------------===//
2448// CastInst SubClass Constructors
2449//===----------------------------------------------------------------------===//
2450
2451/// Check that the construction parameters for a CastInst are correct. This
2452/// could be broken out into the separate constructors but it is useful to have
2453/// it in one place and to eliminate the redundant code for getting the sizes
2454/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002455bool
2456CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457
2458 // Check for type sanity on the arguments
2459 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002460 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2461 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462 return false;
2463
2464 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002465 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2466 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467
2468 // Switch on the opcode provided
2469 switch (op) {
2470 default: return false; // This is an input error
2471 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002472 return SrcTy->isIntOrIntVectorTy() &&
2473 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002475 return SrcTy->isIntOrIntVectorTy() &&
2476 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002477 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002478 return SrcTy->isIntOrIntVectorTy() &&
2479 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002481 return SrcTy->isFPOrFPVectorTy() &&
2482 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002483 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002485 return SrcTy->isFPOrFPVectorTy() &&
2486 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002487 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002489 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002490 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2491 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002492 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2493 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002494 SVTy->getNumElements() == DVTy->getNumElements();
2495 }
2496 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002497 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002500 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2501 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002502 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2503 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002504 SVTy->getNumElements() == DVTy->getNumElements();
2505 }
2506 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002507 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002509 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002511 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512 case Instruction::BitCast:
2513 // BitCast implies a no-op cast of type only. No bits change.
2514 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002515 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 return false;
2517
Duncan Sands55e50902008-01-06 10:12:28 +00002518 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519 // these cases, the cast is okay if the source and destination bit widths
2520 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002521 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 }
2523}
2524
2525TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002526 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002528 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529}
2530
2531TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002532 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002534 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535}
2536
2537ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002538 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002540 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541}
2542
2543ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002544 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002546 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547}
2548SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002549 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002551 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552}
2553
Jeff Cohencc08c832006-12-02 02:22:01 +00002554SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002555 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002557 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558}
2559
2560FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002561 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002563 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564}
2565
2566FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002567 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570}
2571
2572FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002573 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576}
2577
2578FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002579 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
2584UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
2590UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
2596SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002597 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601
2602SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002603 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002609 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
2614FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002615 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618}
2619
2620FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624}
2625
2626FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002627 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630}
2631
2632PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002633 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636}
2637
2638PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002639 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642}
2643
2644IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002645 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002647 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648}
2649
2650IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002651 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002653 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654}
2655
2656BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002657 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002658) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002659 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660}
2661
2662BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002663 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002664) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002665 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002666}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002667
2668//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002669// CmpInst Classes
2670//===----------------------------------------------------------------------===//
2671
Chris Lattneraec33da2010-01-22 06:25:37 +00002672void CmpInst::Anchor() const {}
2673
Nate Begemand2195702008-05-12 19:01:56 +00002674CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002675 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002676 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002677 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002678 OperandTraits<CmpInst>::op_begin(this),
2679 OperandTraits<CmpInst>::operands(this),
2680 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002681 Op<0>() = LHS;
2682 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002683 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002684 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002685}
Gabor Greiff6caff662008-05-10 08:32:32 +00002686
Nate Begemand2195702008-05-12 19:01:56 +00002687CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002688 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002689 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002690 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002691 OperandTraits<CmpInst>::op_begin(this),
2692 OperandTraits<CmpInst>::operands(this),
2693 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002694 Op<0>() = LHS;
2695 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002696 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002697 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002698}
2699
2700CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002701CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002702 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002703 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002704 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002705 if (InsertBefore)
2706 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2707 S1, S2, Name);
2708 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002709 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002710 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002711 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002712
2713 if (InsertBefore)
2714 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2715 S1, S2, Name);
2716 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002717 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002718 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002719}
2720
2721CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002722CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002723 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002724 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002725 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2726 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002727 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002728 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2729 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002730}
2731
2732void CmpInst::swapOperands() {
2733 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2734 IC->swapOperands();
2735 else
2736 cast<FCmpInst>(this)->swapOperands();
2737}
2738
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002739bool CmpInst::isCommutative() const {
2740 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002741 return IC->isCommutative();
2742 return cast<FCmpInst>(this)->isCommutative();
2743}
2744
Duncan Sands95c4ecc2011-01-04 12:52:29 +00002745bool CmpInst::isEquality() const {
2746 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00002747 return IC->isEquality();
2748 return cast<FCmpInst>(this)->isEquality();
2749}
2750
2751
Dan Gohman4e724382008-05-31 02:47:54 +00002752CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002753 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002754 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002755 case ICMP_EQ: return ICMP_NE;
2756 case ICMP_NE: return ICMP_EQ;
2757 case ICMP_UGT: return ICMP_ULE;
2758 case ICMP_ULT: return ICMP_UGE;
2759 case ICMP_UGE: return ICMP_ULT;
2760 case ICMP_ULE: return ICMP_UGT;
2761 case ICMP_SGT: return ICMP_SLE;
2762 case ICMP_SLT: return ICMP_SGE;
2763 case ICMP_SGE: return ICMP_SLT;
2764 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002765
Dan Gohman4e724382008-05-31 02:47:54 +00002766 case FCMP_OEQ: return FCMP_UNE;
2767 case FCMP_ONE: return FCMP_UEQ;
2768 case FCMP_OGT: return FCMP_ULE;
2769 case FCMP_OLT: return FCMP_UGE;
2770 case FCMP_OGE: return FCMP_ULT;
2771 case FCMP_OLE: return FCMP_UGT;
2772 case FCMP_UEQ: return FCMP_ONE;
2773 case FCMP_UNE: return FCMP_OEQ;
2774 case FCMP_UGT: return FCMP_OLE;
2775 case FCMP_ULT: return FCMP_OGE;
2776 case FCMP_UGE: return FCMP_OLT;
2777 case FCMP_ULE: return FCMP_OGT;
2778 case FCMP_ORD: return FCMP_UNO;
2779 case FCMP_UNO: return FCMP_ORD;
2780 case FCMP_TRUE: return FCMP_FALSE;
2781 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002782 }
2783}
2784
Reid Spencer266e42b2006-12-23 06:05:41 +00002785ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2786 switch (pred) {
2787 default: assert(! "Unknown icmp predicate!");
2788 case ICMP_EQ: case ICMP_NE:
2789 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2790 return pred;
2791 case ICMP_UGT: return ICMP_SGT;
2792 case ICMP_ULT: return ICMP_SLT;
2793 case ICMP_UGE: return ICMP_SGE;
2794 case ICMP_ULE: return ICMP_SLE;
2795 }
2796}
2797
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002798ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2799 switch (pred) {
2800 default: assert(! "Unknown icmp predicate!");
2801 case ICMP_EQ: case ICMP_NE:
2802 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2803 return pred;
2804 case ICMP_SGT: return ICMP_UGT;
2805 case ICMP_SLT: return ICMP_ULT;
2806 case ICMP_SGE: return ICMP_UGE;
2807 case ICMP_SLE: return ICMP_ULE;
2808 }
2809}
2810
Reid Spencer0286bc12007-02-28 22:00:54 +00002811/// Initialize a set of values that all satisfy the condition with C.
2812///
2813ConstantRange
2814ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2815 APInt Lower(C);
2816 APInt Upper(C);
2817 uint32_t BitWidth = C.getBitWidth();
2818 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002819 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002820 case ICmpInst::ICMP_EQ: Upper++; break;
2821 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002822 case ICmpInst::ICMP_ULT:
2823 Lower = APInt::getMinValue(BitWidth);
2824 // Check for an empty-set condition.
2825 if (Lower == Upper)
2826 return ConstantRange(BitWidth, /*isFullSet=*/false);
2827 break;
2828 case ICmpInst::ICMP_SLT:
2829 Lower = APInt::getSignedMinValue(BitWidth);
2830 // Check for an empty-set condition.
2831 if (Lower == Upper)
2832 return ConstantRange(BitWidth, /*isFullSet=*/false);
2833 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002834 case ICmpInst::ICMP_UGT:
2835 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002836 // Check for an empty-set condition.
2837 if (Lower == Upper)
2838 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002839 break;
2840 case ICmpInst::ICMP_SGT:
2841 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002842 // Check for an empty-set condition.
2843 if (Lower == Upper)
2844 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002845 break;
2846 case ICmpInst::ICMP_ULE:
2847 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002848 // Check for a full-set condition.
2849 if (Lower == Upper)
2850 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002851 break;
2852 case ICmpInst::ICMP_SLE:
2853 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002854 // Check for a full-set condition.
2855 if (Lower == Upper)
2856 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002857 break;
2858 case ICmpInst::ICMP_UGE:
2859 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002860 // Check for a full-set condition.
2861 if (Lower == Upper)
2862 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002863 break;
2864 case ICmpInst::ICMP_SGE:
2865 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002866 // Check for a full-set condition.
2867 if (Lower == Upper)
2868 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002869 break;
2870 }
2871 return ConstantRange(Lower, Upper);
2872}
2873
Dan Gohman4e724382008-05-31 02:47:54 +00002874CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002875 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002876 default: assert(!"Unknown cmp predicate!");
2877 case ICMP_EQ: case ICMP_NE:
2878 return pred;
2879 case ICMP_SGT: return ICMP_SLT;
2880 case ICMP_SLT: return ICMP_SGT;
2881 case ICMP_SGE: return ICMP_SLE;
2882 case ICMP_SLE: return ICMP_SGE;
2883 case ICMP_UGT: return ICMP_ULT;
2884 case ICMP_ULT: return ICMP_UGT;
2885 case ICMP_UGE: return ICMP_ULE;
2886 case ICMP_ULE: return ICMP_UGE;
2887
Reid Spencerd9436b62006-11-20 01:22:35 +00002888 case FCMP_FALSE: case FCMP_TRUE:
2889 case FCMP_OEQ: case FCMP_ONE:
2890 case FCMP_UEQ: case FCMP_UNE:
2891 case FCMP_ORD: case FCMP_UNO:
2892 return pred;
2893 case FCMP_OGT: return FCMP_OLT;
2894 case FCMP_OLT: return FCMP_OGT;
2895 case FCMP_OGE: return FCMP_OLE;
2896 case FCMP_OLE: return FCMP_OGE;
2897 case FCMP_UGT: return FCMP_ULT;
2898 case FCMP_ULT: return FCMP_UGT;
2899 case FCMP_UGE: return FCMP_ULE;
2900 case FCMP_ULE: return FCMP_UGE;
2901 }
2902}
2903
Reid Spencer266e42b2006-12-23 06:05:41 +00002904bool CmpInst::isUnsigned(unsigned short predicate) {
2905 switch (predicate) {
2906 default: return false;
2907 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2908 case ICmpInst::ICMP_UGE: return true;
2909 }
2910}
2911
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002912bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002913 switch (predicate) {
2914 default: return false;
2915 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2916 case ICmpInst::ICMP_SGE: return true;
2917 }
2918}
2919
2920bool CmpInst::isOrdered(unsigned short predicate) {
2921 switch (predicate) {
2922 default: return false;
2923 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2924 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2925 case FCmpInst::FCMP_ORD: return true;
2926 }
2927}
2928
2929bool CmpInst::isUnordered(unsigned short predicate) {
2930 switch (predicate) {
2931 default: return false;
2932 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2933 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2934 case FCmpInst::FCMP_UNO: return true;
2935 }
2936}
2937
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002938bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2939 switch(predicate) {
2940 default: return false;
2941 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2942 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2943 }
2944}
2945
2946bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2947 switch(predicate) {
2948 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2949 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2950 default: return false;
2951 }
2952}
2953
2954
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002955//===----------------------------------------------------------------------===//
2956// SwitchInst Implementation
2957//===----------------------------------------------------------------------===//
2958
Chris Lattnerbaf00152010-11-17 05:41:46 +00002959void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
2960 assert(Value && Default && NumReserved);
2961 ReservedSpace = NumReserved;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002962 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002963 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002964
Gabor Greif2d3024d2008-05-26 21:33:52 +00002965 OperandList[0] = Value;
2966 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002967}
2968
Chris Lattner2195fc42007-02-24 00:55:48 +00002969/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2970/// switch on and a default destination. The number of additional cases can
2971/// be specified here to make memory allocation more efficient. This
2972/// constructor can also autoinsert before another instruction.
2973SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2974 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002975 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2976 0, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002977 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002978}
2979
2980/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2981/// switch on and a default destination. The number of additional cases can
2982/// be specified here to make memory allocation more efficient. This
2983/// constructor also autoinserts at the end of the specified BasicBlock.
2984SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2985 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002986 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2987 0, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00002988 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00002989}
2990
Misha Brukmanb1c93172005-04-21 23:48:37 +00002991SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerbaf00152010-11-17 05:41:46 +00002992 : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
2993 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
2994 NumOperands = SI.getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002995 Use *OL = OperandList, *InOL = SI.OperandList;
Chris Lattnerbaf00152010-11-17 05:41:46 +00002996 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002997 OL[i] = InOL[i];
2998 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002999 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003000 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003001}
3002
Gordon Henriksen14a55692007-12-10 02:14:30 +00003003SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003004 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003005}
3006
3007
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003008/// addCase - Add an entry to the switch instruction...
3009///
Chris Lattner47ac1872005-02-24 05:32:09 +00003010void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003011 unsigned OpNo = NumOperands;
3012 if (OpNo+2 > ReservedSpace)
3013 resizeOperands(0); // Get more space!
3014 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003015 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003016 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003017 OperandList[OpNo] = OnVal;
3018 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003019}
3020
3021/// removeCase - This method removes the specified successor from the switch
3022/// instruction. Note that this cannot be used to remove the default
3023/// destination (successor #0).
3024///
3025void SwitchInst::removeCase(unsigned idx) {
3026 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003027 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3028
3029 unsigned NumOps = getNumOperands();
3030 Use *OL = OperandList;
3031
3032 // Move everything after this operand down.
3033 //
3034 // FIXME: we could just swap with the end of the list, then erase. However,
3035 // client might not expect this to happen. The code as it is thrashes the
3036 // use/def lists, which is kinda lame.
3037 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3038 OL[i-2] = OL[i];
3039 OL[i-2+1] = OL[i+1];
3040 }
3041
3042 // Nuke the last value.
3043 OL[NumOps-2].set(0);
3044 OL[NumOps-2+1].set(0);
3045 NumOperands = NumOps-2;
3046}
3047
3048/// resizeOperands - resize operands - This adjusts the length of the operands
3049/// list according to the following behavior:
3050/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003051/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003052/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3053/// 3. If NumOps == NumOperands, trim the reserved space.
3054///
3055void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003056 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003057 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003058 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003059 } else if (NumOps*2 > NumOperands) {
3060 // No resize needed.
3061 if (ReservedSpace >= NumOps) return;
3062 } else if (NumOps == NumOperands) {
3063 if (ReservedSpace == NumOps) return;
3064 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003065 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003066 }
3067
3068 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003069 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003070 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003071 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003072 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003073 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003074 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003075 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003076}
3077
3078
3079BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3080 return getSuccessor(idx);
3081}
3082unsigned SwitchInst::getNumSuccessorsV() const {
3083 return getNumSuccessors();
3084}
3085void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3086 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003087}
Chris Lattnerf22be932004-10-15 23:52:53 +00003088
Chris Lattner3ed871f2009-10-27 19:13:16 +00003089//===----------------------------------------------------------------------===//
3090// SwitchInst Implementation
3091//===----------------------------------------------------------------------===//
3092
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003093void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003094 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003095 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003096 ReservedSpace = 1+NumDests;
3097 NumOperands = 1;
3098 OperandList = allocHungoffUses(ReservedSpace);
3099
3100 OperandList[0] = Address;
3101}
3102
3103
3104/// resizeOperands - resize operands - This adjusts the length of the operands
3105/// list according to the following behavior:
3106/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3107/// of operation. This grows the number of ops by 2 times.
3108/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3109/// 3. If NumOps == NumOperands, trim the reserved space.
3110///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003111void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003112 unsigned e = getNumOperands();
3113 if (NumOps == 0) {
3114 NumOps = e*2;
3115 } else if (NumOps*2 > NumOperands) {
3116 // No resize needed.
3117 if (ReservedSpace >= NumOps) return;
3118 } else if (NumOps == NumOperands) {
3119 if (ReservedSpace == NumOps) return;
3120 } else {
3121 return;
3122 }
3123
3124 ReservedSpace = NumOps;
3125 Use *NewOps = allocHungoffUses(NumOps);
3126 Use *OldOps = OperandList;
3127 for (unsigned i = 0; i != e; ++i)
3128 NewOps[i] = OldOps[i];
3129 OperandList = NewOps;
3130 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3131}
3132
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003133IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3134 Instruction *InsertBefore)
3135: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003136 0, 0, InsertBefore) {
3137 init(Address, NumCases);
3138}
3139
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003140IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3141 BasicBlock *InsertAtEnd)
3142: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003143 0, 0, InsertAtEnd) {
3144 init(Address, NumCases);
3145}
3146
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003147IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3148 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003149 allocHungoffUses(IBI.getNumOperands()),
3150 IBI.getNumOperands()) {
3151 Use *OL = OperandList, *InOL = IBI.OperandList;
3152 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3153 OL[i] = InOL[i];
3154 SubclassOptionalData = IBI.SubclassOptionalData;
3155}
3156
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003157IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003158 dropHungoffUses(OperandList);
3159}
3160
3161/// addDestination - Add a destination.
3162///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003163void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003164 unsigned OpNo = NumOperands;
3165 if (OpNo+1 > ReservedSpace)
3166 resizeOperands(0); // Get more space!
3167 // Initialize some new operands.
3168 assert(OpNo < ReservedSpace && "Growing didn't work!");
3169 NumOperands = OpNo+1;
3170 OperandList[OpNo] = DestBB;
3171}
3172
3173/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003174/// indirectbr instruction.
3175void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003176 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3177
3178 unsigned NumOps = getNumOperands();
3179 Use *OL = OperandList;
3180
3181 // Replace this value with the last one.
3182 OL[idx+1] = OL[NumOps-1];
3183
3184 // Nuke the last value.
3185 OL[NumOps-1].set(0);
3186 NumOperands = NumOps-1;
3187}
3188
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003189BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003190 return getSuccessor(idx);
3191}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003192unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003193 return getNumSuccessors();
3194}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003195void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003196 setSuccessor(idx, B);
3197}
3198
3199//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003200// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003201//===----------------------------------------------------------------------===//
3202
Chris Lattnerf22be932004-10-15 23:52:53 +00003203// Define these methods here so vtables don't get emitted into every translation
3204// unit that uses these classes.
3205
Devang Patel11cf3f42009-10-27 22:16:29 +00003206GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3207 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003208}
3209
Devang Patel11cf3f42009-10-27 22:16:29 +00003210BinaryOperator *BinaryOperator::clone_impl() const {
3211 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003212}
3213
Devang Patel11cf3f42009-10-27 22:16:29 +00003214FCmpInst* FCmpInst::clone_impl() const {
3215 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003216}
3217
Devang Patel11cf3f42009-10-27 22:16:29 +00003218ICmpInst* ICmpInst::clone_impl() const {
3219 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003220}
3221
Devang Patel11cf3f42009-10-27 22:16:29 +00003222ExtractValueInst *ExtractValueInst::clone_impl() const {
3223 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003224}
3225
Devang Patel11cf3f42009-10-27 22:16:29 +00003226InsertValueInst *InsertValueInst::clone_impl() const {
3227 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003228}
3229
Devang Patel11cf3f42009-10-27 22:16:29 +00003230AllocaInst *AllocaInst::clone_impl() const {
3231 return new AllocaInst(getAllocatedType(),
3232 (Value*)getOperand(0),
3233 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003234}
3235
Devang Patel11cf3f42009-10-27 22:16:29 +00003236LoadInst *LoadInst::clone_impl() const {
3237 return new LoadInst(getOperand(0),
3238 Twine(), isVolatile(),
3239 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003240}
3241
Devang Patel11cf3f42009-10-27 22:16:29 +00003242StoreInst *StoreInst::clone_impl() const {
3243 return new StoreInst(getOperand(0), getOperand(1),
3244 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003245}
3246
Devang Patel11cf3f42009-10-27 22:16:29 +00003247TruncInst *TruncInst::clone_impl() const {
3248 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003249}
3250
Devang Patel11cf3f42009-10-27 22:16:29 +00003251ZExtInst *ZExtInst::clone_impl() const {
3252 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003253}
3254
Devang Patel11cf3f42009-10-27 22:16:29 +00003255SExtInst *SExtInst::clone_impl() const {
3256 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003257}
3258
Devang Patel11cf3f42009-10-27 22:16:29 +00003259FPTruncInst *FPTruncInst::clone_impl() const {
3260 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003261}
3262
Devang Patel11cf3f42009-10-27 22:16:29 +00003263FPExtInst *FPExtInst::clone_impl() const {
3264 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003265}
3266
Devang Patel11cf3f42009-10-27 22:16:29 +00003267UIToFPInst *UIToFPInst::clone_impl() const {
3268 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003269}
3270
Devang Patel11cf3f42009-10-27 22:16:29 +00003271SIToFPInst *SIToFPInst::clone_impl() const {
3272 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003273}
3274
Devang Patel11cf3f42009-10-27 22:16:29 +00003275FPToUIInst *FPToUIInst::clone_impl() const {
3276 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003277}
3278
Devang Patel11cf3f42009-10-27 22:16:29 +00003279FPToSIInst *FPToSIInst::clone_impl() const {
3280 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003281}
3282
Devang Patel11cf3f42009-10-27 22:16:29 +00003283PtrToIntInst *PtrToIntInst::clone_impl() const {
3284 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003285}
3286
Devang Patel11cf3f42009-10-27 22:16:29 +00003287IntToPtrInst *IntToPtrInst::clone_impl() const {
3288 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003289}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003290
Devang Patel11cf3f42009-10-27 22:16:29 +00003291BitCastInst *BitCastInst::clone_impl() const {
3292 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003293}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003294
Devang Patel11cf3f42009-10-27 22:16:29 +00003295CallInst *CallInst::clone_impl() const {
3296 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003297}
3298
Devang Patel11cf3f42009-10-27 22:16:29 +00003299SelectInst *SelectInst::clone_impl() const {
3300 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003301}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003302
Devang Patel11cf3f42009-10-27 22:16:29 +00003303VAArgInst *VAArgInst::clone_impl() const {
3304 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003305}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003306
Devang Patel11cf3f42009-10-27 22:16:29 +00003307ExtractElementInst *ExtractElementInst::clone_impl() const {
3308 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003309}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003310
Devang Patel11cf3f42009-10-27 22:16:29 +00003311InsertElementInst *InsertElementInst::clone_impl() const {
3312 return InsertElementInst::Create(getOperand(0),
3313 getOperand(1),
3314 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003315}
3316
Devang Patel11cf3f42009-10-27 22:16:29 +00003317ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3318 return new ShuffleVectorInst(getOperand(0),
3319 getOperand(1),
3320 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003321}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003322
Devang Patel11cf3f42009-10-27 22:16:29 +00003323PHINode *PHINode::clone_impl() const {
3324 return new PHINode(*this);
3325}
3326
3327ReturnInst *ReturnInst::clone_impl() const {
3328 return new(getNumOperands()) ReturnInst(*this);
3329}
3330
3331BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003332 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003333 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003334}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003335
Devang Patel11cf3f42009-10-27 22:16:29 +00003336SwitchInst *SwitchInst::clone_impl() const {
3337 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003338}
3339
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003340IndirectBrInst *IndirectBrInst::clone_impl() const {
3341 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003342}
3343
3344
Devang Patel11cf3f42009-10-27 22:16:29 +00003345InvokeInst *InvokeInst::clone_impl() const {
3346 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003347}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003348
Devang Patel11cf3f42009-10-27 22:16:29 +00003349UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003350 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003351 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003352}
3353
Devang Patel11cf3f42009-10-27 22:16:29 +00003354UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003355 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003356 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003357}