blob: f4519d4dc2964480fba529c998f767c6a445bff0 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
20#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000021#include "llvm/Support/ConstantRange.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022using namespace llvm;
23
Chris Lattnerf7b6d312005-05-06 20:26:43 +000024unsigned CallSite::getCallingConv() const {
25 if (CallInst *CI = dyn_cast<CallInst>(I))
26 return CI->getCallingConv();
27 else
28 return cast<InvokeInst>(I)->getCallingConv();
29}
30void CallSite::setCallingConv(unsigned CC) {
31 if (CallInst *CI = dyn_cast<CallInst>(I))
32 CI->setCallingConv(CC);
33 else
34 cast<InvokeInst>(I)->setCallingConv(CC);
35}
36
37
Chris Lattner1c12a882006-06-21 16:53:47 +000038
39
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000040//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000041// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
Chris Lattner1c12a882006-06-21 16:53:47 +000044// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
48// Out of line virtual method, so the vtable, etc has a home.
49UnaryInstruction::~UnaryInstruction() {
50}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000051
52
53//===----------------------------------------------------------------------===//
54// PHINode Class
55//===----------------------------------------------------------------------===//
56
57PHINode::PHINode(const PHINode &PN)
58 : Instruction(PN.getType(), Instruction::PHI,
59 new Use[PN.getNumOperands()], PN.getNumOperands()),
60 ReservedSpace(PN.getNumOperands()) {
61 Use *OL = OperandList;
62 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
63 OL[i].init(PN.getOperand(i), this);
64 OL[i+1].init(PN.getOperand(i+1), this);
65 }
66}
67
68PHINode::~PHINode() {
69 delete [] OperandList;
70}
71
72// removeIncomingValue - Remove an incoming value. This is useful if a
73// predecessor basic block is deleted.
74Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
75 unsigned NumOps = getNumOperands();
76 Use *OL = OperandList;
77 assert(Idx*2 < NumOps && "BB not in PHI node!");
78 Value *Removed = OL[Idx*2];
79
80 // Move everything after this operand down.
81 //
82 // FIXME: we could just swap with the end of the list, then erase. However,
83 // client might not expect this to happen. The code as it is thrashes the
84 // use/def lists, which is kinda lame.
85 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
86 OL[i-2] = OL[i];
87 OL[i-2+1] = OL[i+1];
88 }
89
90 // Nuke the last value.
91 OL[NumOps-2].set(0);
92 OL[NumOps-2+1].set(0);
93 NumOperands = NumOps-2;
94
95 // If the PHI node is dead, because it has zero entries, nuke it now.
96 if (NumOps == 2 && DeletePHIIfEmpty) {
97 // If anyone is using this PHI, make them use a dummy value instead...
98 replaceAllUsesWith(UndefValue::get(getType()));
99 eraseFromParent();
100 }
101 return Removed;
102}
103
104/// resizeOperands - resize operands - This adjusts the length of the operands
105/// list according to the following behavior:
106/// 1. If NumOps == 0, grow the operand list in response to a push_back style
107/// of operation. This grows the number of ops by 1.5 times.
108/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
109/// 3. If NumOps == NumOperands, trim the reserved space.
110///
111void PHINode::resizeOperands(unsigned NumOps) {
112 if (NumOps == 0) {
113 NumOps = (getNumOperands())*3/2;
114 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
115 } else if (NumOps*2 > NumOperands) {
116 // No resize needed.
117 if (ReservedSpace >= NumOps) return;
118 } else if (NumOps == NumOperands) {
119 if (ReservedSpace == NumOps) return;
120 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000121 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 }
123
124 ReservedSpace = NumOps;
125 Use *NewOps = new Use[NumOps];
126 Use *OldOps = OperandList;
127 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
128 NewOps[i].init(OldOps[i], this);
129 OldOps[i].set(0);
130 }
131 delete [] OldOps;
132 OperandList = NewOps;
133}
134
Nate Begemanb3923212005-08-04 23:24:19 +0000135/// hasConstantValue - If the specified PHI node always merges together the same
136/// value, return the value, otherwise return null.
137///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000138Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000139 // If the PHI node only has one incoming value, eliminate the PHI node...
140 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000141 if (getIncomingValue(0) != this) // not X = phi X
142 return getIncomingValue(0);
143 else
144 return UndefValue::get(getType()); // Self cycle is dead.
145
Nate Begemanb3923212005-08-04 23:24:19 +0000146 // Otherwise if all of the incoming values are the same for the PHI, replace
147 // the PHI node with the incoming value.
148 //
149 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000150 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000151 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000152 if (isa<UndefValue>(getIncomingValue(i)))
153 HasUndefInput = true;
154 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000155 if (InVal && getIncomingValue(i) != InVal)
156 return 0; // Not the same, bail out.
157 else
158 InVal = getIncomingValue(i);
159
160 // The only case that could cause InVal to be null is if we have a PHI node
161 // that only has entries for itself. In this case, there is no entry into the
162 // loop, so kill the PHI.
163 //
164 if (InVal == 0) InVal = UndefValue::get(getType());
165
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000166 // If we have a PHI node like phi(X, undef, X), where X is defined by some
167 // instruction, we cannot always return X as the result of the PHI node. Only
168 // do this if X is not an instruction (thus it must dominate the PHI block),
169 // or if the client is prepared to deal with this possibility.
170 if (HasUndefInput && !AllowNonDominatingInstruction)
171 if (Instruction *IV = dyn_cast<Instruction>(InVal))
172 // If it's in the entry block, it dominates everything.
Chris Lattner37774af2005-08-05 01:03:27 +0000173 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
174 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 return 0; // Cannot guarantee that InVal dominates this PHINode.
176
Nate Begemanb3923212005-08-04 23:24:19 +0000177 // All of the incoming values are the same, return the value now.
178 return InVal;
179}
180
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181
182//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000183// CallInst Implementation
184//===----------------------------------------------------------------------===//
185
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000186CallInst::~CallInst() {
187 delete [] OperandList;
188}
189
Chris Lattner054ba2c2007-02-13 00:58:44 +0000190void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
191 NumOperands = NumParams+1;
192 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000193 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000194
Misha Brukmanb1c93172005-04-21 23:48:37 +0000195 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000196 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000197 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000198
Chris Lattner054ba2c2007-02-13 00:58:44 +0000199 assert((NumParams == FTy->getNumParams() ||
200 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000201 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000202 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000203 assert((i >= FTy->getNumParams() ||
204 FTy->getParamType(i) == Params[i]->getType()) &&
205 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000206 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000207 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000208}
209
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000210void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
211 NumOperands = 3;
212 Use *OL = OperandList = new Use[3];
213 OL[0].init(Func, this);
214 OL[1].init(Actual1, this);
215 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000216
217 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000218 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000219 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000220
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000221 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000222 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000223 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000224 assert((0 >= FTy->getNumParams() ||
225 FTy->getParamType(0) == Actual1->getType()) &&
226 "Calling a function with a bad signature!");
227 assert((1 >= FTy->getNumParams() ||
228 FTy->getParamType(1) == Actual2->getType()) &&
229 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000230}
231
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232void CallInst::init(Value *Func, Value *Actual) {
233 NumOperands = 2;
234 Use *OL = OperandList = new Use[2];
235 OL[0].init(Func, this);
236 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000237
238 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000240 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000241
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000242 assert((FTy->getNumParams() == 1 ||
243 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000244 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000245 assert((0 == FTy->getNumParams() ||
246 FTy->getParamType(0) == Actual->getType()) &&
247 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248}
249
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000250void CallInst::init(Value *Func) {
251 NumOperands = 1;
252 Use *OL = OperandList = new Use[1];
253 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000254
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000255 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000256 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000257 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000259 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260}
261
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000262CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000263 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
265 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000266 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000267 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000268 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000269}
270CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
271 const std::string &Name, Instruction *InsertBefore)
272: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
273 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000274 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000275 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000276 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000277}
278
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
280 const std::string &Name, Instruction *InsertBefore)
281 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
282 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000283 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000285 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286}
287
288CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
289 const std::string &Name, BasicBlock *InsertAtEnd)
290 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
291 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000292 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000294 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000295}
296
297CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000298 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
300 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000301 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000303 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304}
305
306CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
307 BasicBlock *InsertAtEnd)
308 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
309 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000310 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000312 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
315CallInst::CallInst(Value *Func, const std::string &Name,
316 Instruction *InsertBefore)
317 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
318 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000319 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000321 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322}
323
324CallInst::CallInst(Value *Func, const std::string &Name,
325 BasicBlock *InsertAtEnd)
326 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
327 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000330 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
Misha Brukmanb1c93172005-04-21 23:48:37 +0000333CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000334 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
335 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000336 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000337 Use *OL = OperandList;
338 Use *InOL = CI.OperandList;
339 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
340 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
342
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343
344//===----------------------------------------------------------------------===//
345// InvokeInst Implementation
346//===----------------------------------------------------------------------===//
347
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000348InvokeInst::~InvokeInst() {
349 delete [] OperandList;
350}
351
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000353 Value* const *Args, unsigned NumArgs) {
354 NumOperands = 3+NumArgs;
355 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000356 OL[0].init(Fn, this);
357 OL[1].init(IfNormal, this);
358 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000359 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000361 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000362
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000363 assert((NumArgs == FTy->getNumParams()) ||
364 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000366
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000367 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000368 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000369 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000370 "Invoking a function with a bad signature!");
371
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000372 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000373 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374}
375
376InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
377 BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000378 Value* const *Args, unsigned NumArgs,
379 const std::string &Name, Instruction *InsertBefore)
380 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
381 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000382 Instruction::Invoke, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000383 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000384 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000385}
386
387InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
388 BasicBlock *IfException,
389 Value* const *Args, unsigned NumArgs,
390 const std::string &Name, BasicBlock *InsertAtEnd)
391 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
392 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000393 Instruction::Invoke, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000394 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000395 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000396}
397
Misha Brukmanb1c93172005-04-21 23:48:37 +0000398InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000399 : TerminatorInst(II.getType(), Instruction::Invoke,
400 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000401 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402 Use *OL = OperandList, *InOL = II.OperandList;
403 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
404 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000405}
406
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000407BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
408 return getSuccessor(idx);
409}
410unsigned InvokeInst::getNumSuccessorsV() const {
411 return getNumSuccessors();
412}
413void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
414 return setSuccessor(idx, B);
415}
416
417
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000418//===----------------------------------------------------------------------===//
419// ReturnInst Implementation
420//===----------------------------------------------------------------------===//
421
Chris Lattner2195fc42007-02-24 00:55:48 +0000422ReturnInst::ReturnInst(const ReturnInst &RI)
423 : TerminatorInst(Type::VoidTy, Instruction::Ret,
424 &RetVal, RI.getNumOperands()) {
425 if (RI.getNumOperands())
426 RetVal.init(RI.RetVal, this);
427}
428
429ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
430 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
431 init(retVal);
432}
433ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
434 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
435 init(retVal);
436}
437ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
438 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
439}
440
441
442
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000443void ReturnInst::init(Value *retVal) {
444 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000445 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000446 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000447 NumOperands = 1;
448 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000449 }
450}
451
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000452unsigned ReturnInst::getNumSuccessorsV() const {
453 return getNumSuccessors();
454}
455
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000456// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
457// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000458void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000459 assert(0 && "ReturnInst has no successors!");
460}
461
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000462BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
463 assert(0 && "ReturnInst has no successors!");
464 abort();
465 return 0;
466}
467
468
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000469//===----------------------------------------------------------------------===//
470// UnwindInst Implementation
471//===----------------------------------------------------------------------===//
472
Chris Lattner2195fc42007-02-24 00:55:48 +0000473UnwindInst::UnwindInst(Instruction *InsertBefore)
474 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
475}
476UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
477 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
478}
479
480
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000481unsigned UnwindInst::getNumSuccessorsV() const {
482 return getNumSuccessors();
483}
484
485void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000486 assert(0 && "UnwindInst has no successors!");
487}
488
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000489BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
490 assert(0 && "UnwindInst has no successors!");
491 abort();
492 return 0;
493}
494
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000495//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000496// UnreachableInst Implementation
497//===----------------------------------------------------------------------===//
498
Chris Lattner2195fc42007-02-24 00:55:48 +0000499UnreachableInst::UnreachableInst(Instruction *InsertBefore)
500 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
501}
502UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
503 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
504}
505
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000506unsigned UnreachableInst::getNumSuccessorsV() const {
507 return getNumSuccessors();
508}
509
510void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
511 assert(0 && "UnwindInst has no successors!");
512}
513
514BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
515 assert(0 && "UnwindInst has no successors!");
516 abort();
517 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000518}
519
520//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000521// BranchInst Implementation
522//===----------------------------------------------------------------------===//
523
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000524void BranchInst::AssertOK() {
525 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000526 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000527 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528}
529
Chris Lattner2195fc42007-02-24 00:55:48 +0000530BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
531 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
532 assert(IfTrue != 0 && "Branch destination may not be null!");
533 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
534}
535BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
536 Instruction *InsertBefore)
537: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
538 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
539 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
540 Ops[2].init(Cond, this);
541#ifndef NDEBUG
542 AssertOK();
543#endif
544}
545
546BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
547 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
548 assert(IfTrue != 0 && "Branch destination may not be null!");
549 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
550}
551
552BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
553 BasicBlock *InsertAtEnd)
554 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
555 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
556 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
557 Ops[2].init(Cond, this);
558#ifndef NDEBUG
559 AssertOK();
560#endif
561}
562
563
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000564BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000565 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000566 OperandList[0].init(BI.getOperand(0), this);
567 if (BI.getNumOperands() != 1) {
568 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
569 OperandList[1].init(BI.getOperand(1), this);
570 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000571 }
572}
573
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
575 return getSuccessor(idx);
576}
577unsigned BranchInst::getNumSuccessorsV() const {
578 return getNumSuccessors();
579}
580void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
581 setSuccessor(idx, B);
582}
583
584
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000585//===----------------------------------------------------------------------===//
586// AllocationInst Implementation
587//===----------------------------------------------------------------------===//
588
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000589static Value *getAISize(Value *Amt) {
590 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000591 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000592 else {
593 assert(!isa<BasicBlock>(Amt) &&
594 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000595 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000596 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000597 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000598 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599}
600
Misha Brukmanb1c93172005-04-21 23:48:37 +0000601AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000602 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000603 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000604 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000605 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000606 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000608 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609}
610
Misha Brukmanb1c93172005-04-21 23:48:37 +0000611AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000612 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000614 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000615 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000616 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000617 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000618 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000619}
620
Chris Lattner1c12a882006-06-21 16:53:47 +0000621// Out of line virtual method, so the vtable, etc has a home.
622AllocationInst::~AllocationInst() {
623}
624
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000625bool AllocationInst::isArrayAllocation() const {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000626 if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0)))
627 return CUI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629}
630
631const Type *AllocationInst::getAllocatedType() const {
632 return getType()->getElementType();
633}
634
635AllocaInst::AllocaInst(const AllocaInst &AI)
636 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000637 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000638}
639
640MallocInst::MallocInst(const MallocInst &MI)
641 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000642 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000643}
644
645//===----------------------------------------------------------------------===//
646// FreeInst Implementation
647//===----------------------------------------------------------------------===//
648
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000649void FreeInst::AssertOK() {
650 assert(isa<PointerType>(getOperand(0)->getType()) &&
651 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000652}
653
654FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000655 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000657}
658
659FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000660 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000662}
663
664
665//===----------------------------------------------------------------------===//
666// LoadInst Implementation
667//===----------------------------------------------------------------------===//
668
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000670 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000671 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000672}
673
674LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000675 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000676 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000677 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000679 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000680}
681
682LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000683 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000684 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000685 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000686 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000687 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688}
689
690LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
691 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000692 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000693 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000694 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000695 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000696 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000697}
698
699LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
700 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000701 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000702 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000703 setVolatile(isVolatile);
704 AssertOK();
705 setName(Name);
706}
707
708
709
710LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000711 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
712 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000713 setVolatile(false);
714 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000715 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000716}
717
718LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000719 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
720 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000721 setVolatile(false);
722 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000723 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000724}
725
726LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
727 Instruction *InsertBef)
728: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000729 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000730 setVolatile(isVolatile);
731 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000732 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000733}
734
735LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
736 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000737 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
738 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000739 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000740 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000741 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000742}
743
744
745//===----------------------------------------------------------------------===//
746// StoreInst Implementation
747//===----------------------------------------------------------------------===//
748
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000749void StoreInst::AssertOK() {
750 assert(isa<PointerType>(getOperand(1)->getType()) &&
751 "Ptr must have pointer type!");
752 assert(getOperand(0)->getType() ==
753 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000754 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000755}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000756
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000757
758StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000759 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000760 Ops[0].init(val, this);
761 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000762 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000763 AssertOK();
764}
765
766StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000767 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000768 Ops[0].init(val, this);
769 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000770 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771 AssertOK();
772}
773
Misha Brukmanb1c93172005-04-21 23:48:37 +0000774StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000776 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777 Ops[0].init(val, this);
778 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000779 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780 AssertOK();
781}
782
Misha Brukmanb1c93172005-04-21 23:48:37 +0000783StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000785 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000786 Ops[0].init(val, this);
787 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000788 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790}
791
792//===----------------------------------------------------------------------===//
793// GetElementPtrInst Implementation
794//===----------------------------------------------------------------------===//
795
796// checkType - Simple wrapper function to give a better assertion failure
797// message on bad indexes for a gep instruction.
798//
799static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000800 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000801 return Ty;
802}
803
Chris Lattner79807c3d2007-01-31 19:47:18 +0000804void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
805 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000806 Use *OL = OperandList = new Use[NumOperands];
807 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000808
Chris Lattner79807c3d2007-01-31 19:47:18 +0000809 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811}
812
813void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000814 NumOperands = 3;
815 Use *OL = OperandList = new Use[3];
816 OL[0].init(Ptr, this);
817 OL[1].init(Idx0, this);
818 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000819}
820
Chris Lattner82981202005-05-03 05:43:30 +0000821void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
822 NumOperands = 2;
823 Use *OL = OperandList = new Use[2];
824 OL[0].init(Ptr, this);
825 OL[1].init(Idx, this);
826}
827
Chris Lattner79807c3d2007-01-31 19:47:18 +0000828
829GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
830 unsigned NumIdx,
831 const std::string &Name, Instruction *InBe)
832: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000833 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000834 GetElementPtr, 0, 0, InBe) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000835 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000836 setName(Name);
Chris Lattner79807c3d2007-01-31 19:47:18 +0000837}
838
839GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
840 unsigned NumIdx,
841 const std::string &Name, BasicBlock *IAE)
842: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000843 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000844 GetElementPtr, 0, 0, IAE) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000845 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000846 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000847}
848
Chris Lattner82981202005-05-03 05:43:30 +0000849GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
850 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000851 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
852 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000853 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000854 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000855}
856
857GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
858 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
860 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000861 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000862 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000863}
864
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000865GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
866 const std::string &Name, Instruction *InBe)
867 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
868 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000869 GetElementPtr, 0, 0, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000870 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000871 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000872}
873
874GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000875 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000876 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
877 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000878 GetElementPtr, 0, 0, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000880 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000881}
882
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000883GetElementPtrInst::~GetElementPtrInst() {
884 delete[] OperandList;
885}
886
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887// getIndexedType - Returns the type of the element that would be loaded with
888// a load instruction with the specified parameters.
889//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000890// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000891// pointer type.
892//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000893const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000894 Value* const *Idxs,
895 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000896 bool AllowCompositeLeaf) {
897 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
898
899 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000900 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901 if (AllowCompositeLeaf ||
902 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
903 return cast<PointerType>(Ptr)->getElementType();
904 else
905 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000906
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907 unsigned CurIdx = 0;
908 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000909 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000910 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
911 return 0; // Can't load a whole structure or array!?!?
912 }
913
Chris Lattner302116a2007-01-31 04:40:28 +0000914 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000915 if (isa<PointerType>(CT) && CurIdx != 1)
916 return 0; // Can only index into pointer types at the first index!
917 if (!CT->indexValid(Index)) return 0;
918 Ptr = CT->getTypeAtIndex(Index);
919
920 // If the new type forwards to another type, then it is in the middle
921 // of being refined to another type (and hence, may have dropped all
922 // references to what it was using before). So, use the new forwarded
923 // type.
924 if (const Type * Ty = Ptr->getForwardedType()) {
925 Ptr = Ty;
926 }
927 }
Chris Lattner302116a2007-01-31 04:40:28 +0000928 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929}
930
Misha Brukmanb1c93172005-04-21 23:48:37 +0000931const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000932 Value *Idx0, Value *Idx1,
933 bool AllowCompositeLeaf) {
934 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
935 if (!PTy) return 0; // Type isn't a pointer type!
936
937 // Check the pointer index.
938 if (!PTy->indexValid(Idx0)) return 0;
939
940 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
941 if (!CT || !CT->indexValid(Idx1)) return 0;
942
943 const Type *ElTy = CT->getTypeAtIndex(Idx1);
944 if (AllowCompositeLeaf || ElTy->isFirstClassType())
945 return ElTy;
946 return 0;
947}
948
Chris Lattner82981202005-05-03 05:43:30 +0000949const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
950 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
951 if (!PTy) return 0; // Type isn't a pointer type!
952
953 // Check the pointer index.
954 if (!PTy->indexValid(Idx)) return 0;
955
Chris Lattnerc2233332005-05-03 16:44:45 +0000956 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000957}
958
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000959//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000960// ExtractElementInst Implementation
961//===----------------------------------------------------------------------===//
962
963ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000964 const std::string &Name,
965 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000966 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000967 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000968 assert(isValidOperands(Val, Index) &&
969 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000970 Ops[0].init(Val, this);
971 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000972 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +0000973}
974
Chris Lattner65511ff2006-10-05 06:24:58 +0000975ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
976 const std::string &Name,
977 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000978 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000979 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000980 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000981 assert(isValidOperands(Val, Index) &&
982 "Invalid extractelement instruction operands!");
983 Ops[0].init(Val, this);
984 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000985 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +0000986}
987
988
Robert Bocchino23004482006-01-10 19:05:34 +0000989ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000990 const std::string &Name,
991 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000992 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000993 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +0000994 assert(isValidOperands(Val, Index) &&
995 "Invalid extractelement instruction operands!");
996
Robert Bocchino23004482006-01-10 19:05:34 +0000997 Ops[0].init(Val, this);
998 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000999 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001000}
1001
Chris Lattner65511ff2006-10-05 06:24:58 +00001002ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1003 const std::string &Name,
1004 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001005 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001006 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001007 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001008 assert(isValidOperands(Val, Index) &&
1009 "Invalid extractelement instruction operands!");
1010
1011 Ops[0].init(Val, this);
1012 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001013 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001014}
1015
1016
Chris Lattner54865b32006-04-08 04:05:48 +00001017bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001018 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001019 return false;
1020 return true;
1021}
1022
1023
Robert Bocchino23004482006-01-10 19:05:34 +00001024//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001025// InsertElementInst Implementation
1026//===----------------------------------------------------------------------===//
1027
Chris Lattner0875d942006-04-14 22:20:32 +00001028InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1029 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1030 Ops[0].init(IE.Ops[0], this);
1031 Ops[1].init(IE.Ops[1], this);
1032 Ops[2].init(IE.Ops[2], this);
1033}
Chris Lattner54865b32006-04-08 04:05:48 +00001034InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001035 const std::string &Name,
1036 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001037 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001038 assert(isValidOperands(Vec, Elt, Index) &&
1039 "Invalid insertelement instruction operands!");
1040 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001041 Ops[1].init(Elt, this);
1042 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001043 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001044}
1045
Chris Lattner65511ff2006-10-05 06:24:58 +00001046InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1047 const std::string &Name,
1048 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001049 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001050 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001051 assert(isValidOperands(Vec, Elt, Index) &&
1052 "Invalid insertelement instruction operands!");
1053 Ops[0].init(Vec, this);
1054 Ops[1].init(Elt, this);
1055 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001056 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001057}
1058
1059
Chris Lattner54865b32006-04-08 04:05:48 +00001060InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001061 const std::string &Name,
1062 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001063 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001064 assert(isValidOperands(Vec, Elt, Index) &&
1065 "Invalid insertelement instruction operands!");
1066
1067 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001068 Ops[1].init(Elt, this);
1069 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001070 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001071}
1072
Chris Lattner65511ff2006-10-05 06:24:58 +00001073InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1074 const std::string &Name,
1075 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001076: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001077 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001078 assert(isValidOperands(Vec, Elt, Index) &&
1079 "Invalid insertelement instruction operands!");
1080
1081 Ops[0].init(Vec, this);
1082 Ops[1].init(Elt, this);
1083 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001084 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001085}
1086
Chris Lattner54865b32006-04-08 04:05:48 +00001087bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1088 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001089 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001090 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001091
Reid Spencerd84d35b2007-02-15 02:26:10 +00001092 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Chris Lattner54865b32006-04-08 04:05:48 +00001093 return false;// Second operand of insertelement must be packed element type.
1094
Reid Spencer8d9336d2006-12-31 05:26:44 +00001095 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001096 return false; // Third operand of insertelement must be uint.
1097 return true;
1098}
1099
1100
Robert Bocchinoca27f032006-01-17 20:07:22 +00001101//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001102// ShuffleVectorInst Implementation
1103//===----------------------------------------------------------------------===//
1104
Chris Lattner0875d942006-04-14 22:20:32 +00001105ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1106 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1107 Ops[0].init(SV.Ops[0], this);
1108 Ops[1].init(SV.Ops[1], this);
1109 Ops[2].init(SV.Ops[2], this);
1110}
1111
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001112ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1113 const std::string &Name,
1114 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001115 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001116 assert(isValidOperands(V1, V2, Mask) &&
1117 "Invalid shuffle vector instruction operands!");
1118 Ops[0].init(V1, this);
1119 Ops[1].init(V2, this);
1120 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001121 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001122}
1123
1124ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1125 const std::string &Name,
1126 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001127 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001128 assert(isValidOperands(V1, V2, Mask) &&
1129 "Invalid shuffle vector instruction operands!");
1130
1131 Ops[0].init(V1, this);
1132 Ops[1].init(V2, this);
1133 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001134 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001135}
1136
1137bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1138 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001139 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001140 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001141 if (!isa<VectorType>(Mask->getType()) ||
1142 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1143 cast<VectorType>(Mask->getType())->getNumElements() !=
1144 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001145 return false;
1146 return true;
1147}
1148
1149
1150//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001151// BinaryOperator Class
1152//===----------------------------------------------------------------------===//
1153
Chris Lattner2195fc42007-02-24 00:55:48 +00001154BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1155 const Type *Ty, const std::string &Name,
1156 Instruction *InsertBefore)
1157 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1158 Ops[0].init(S1, this);
1159 Ops[1].init(S2, this);
1160 init(iType);
1161 setName(Name);
1162}
1163
1164BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1165 const Type *Ty, const std::string &Name,
1166 BasicBlock *InsertAtEnd)
1167 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1168 Ops[0].init(S1, this);
1169 Ops[1].init(S2, this);
1170 init(iType);
1171 setName(Name);
1172}
1173
1174
1175void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001176 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001177 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001178 assert(LHS->getType() == RHS->getType() &&
1179 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001180#ifndef NDEBUG
1181 switch (iType) {
1182 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001183 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001184 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001185 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001186 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001187 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001188 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001189 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001190 case UDiv:
1191 case SDiv:
1192 assert(getType() == LHS->getType() &&
1193 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001194 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1195 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001196 "Incorrect operand type (not integer) for S/UDIV");
1197 break;
1198 case FDiv:
1199 assert(getType() == LHS->getType() &&
1200 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001201 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1202 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001203 && "Incorrect operand type (not floating point) for FDIV");
1204 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001205 case URem:
1206 case SRem:
1207 assert(getType() == LHS->getType() &&
1208 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001209 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1210 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001211 "Incorrect operand type (not integer) for S/UREM");
1212 break;
1213 case FRem:
1214 assert(getType() == LHS->getType() &&
1215 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001216 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1217 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001218 && "Incorrect operand type (not floating point) for FREM");
1219 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001220 case Shl:
1221 case LShr:
1222 case AShr:
1223 assert(getType() == LHS->getType() &&
1224 "Shift operation should return same type as operands!");
1225 assert(getType()->isInteger() &&
1226 "Shift operation requires integer operands");
1227 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001228 case And: case Or:
1229 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001230 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001231 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001232 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001233 (isa<VectorType>(getType()) &&
1234 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001235 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001236 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001237 default:
1238 break;
1239 }
1240#endif
1241}
1242
1243BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001244 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001245 Instruction *InsertBefore) {
1246 assert(S1->getType() == S2->getType() &&
1247 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001248 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001249}
1250
1251BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001252 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001253 BasicBlock *InsertAtEnd) {
1254 BinaryOperator *Res = create(Op, S1, S2, Name);
1255 InsertAtEnd->getInstList().push_back(Res);
1256 return Res;
1257}
1258
1259BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1260 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001261 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1262 return new BinaryOperator(Instruction::Sub,
1263 zero, Op,
1264 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001265}
1266
1267BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1268 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001269 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1270 return new BinaryOperator(Instruction::Sub,
1271 zero, Op,
1272 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001273}
1274
1275BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1276 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001277 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001278 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001279 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001280 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001281 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001282 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001283 }
1284
1285 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001286 Op->getType(), Name, InsertBefore);
1287}
1288
1289BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1290 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001291 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001292 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001293 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001294 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001295 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001296 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001297 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001298 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001299 }
1300
1301 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001302 Op->getType(), Name, InsertAtEnd);
1303}
1304
1305
1306// isConstantAllOnes - Helper function for several functions below
1307static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001308 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001309}
1310
1311bool BinaryOperator::isNeg(const Value *V) {
1312 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1313 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001314 return Bop->getOperand(0) ==
1315 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001316 return false;
1317}
1318
1319bool BinaryOperator::isNot(const Value *V) {
1320 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1321 return (Bop->getOpcode() == Instruction::Xor &&
1322 (isConstantAllOnes(Bop->getOperand(1)) ||
1323 isConstantAllOnes(Bop->getOperand(0))));
1324 return false;
1325}
1326
Chris Lattner2c7d1772005-04-24 07:28:37 +00001327Value *BinaryOperator::getNegArgument(Value *BinOp) {
1328 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1329 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001330}
1331
Chris Lattner2c7d1772005-04-24 07:28:37 +00001332const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1333 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001334}
1335
Chris Lattner2c7d1772005-04-24 07:28:37 +00001336Value *BinaryOperator::getNotArgument(Value *BinOp) {
1337 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1338 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1339 Value *Op0 = BO->getOperand(0);
1340 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001341 if (isConstantAllOnes(Op0)) return Op1;
1342
1343 assert(isConstantAllOnes(Op1));
1344 return Op0;
1345}
1346
Chris Lattner2c7d1772005-04-24 07:28:37 +00001347const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1348 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001349}
1350
1351
1352// swapOperands - Exchange the two operands to this instruction. This
1353// instruction is safe to use on any binary instruction and does not
1354// modify the semantics of the instruction. If the instruction is
1355// order dependent (SetLT f.e.) the opcode is changed.
1356//
1357bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001358 if (!isCommutative())
1359 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001360 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001361 return false;
1362}
1363
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001364//===----------------------------------------------------------------------===//
1365// CastInst Class
1366//===----------------------------------------------------------------------===//
1367
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001368// Just determine if this cast only deals with integral->integral conversion.
1369bool CastInst::isIntegerCast() const {
1370 switch (getOpcode()) {
1371 default: return false;
1372 case Instruction::ZExt:
1373 case Instruction::SExt:
1374 case Instruction::Trunc:
1375 return true;
1376 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001377 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001378 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001379}
1380
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001381bool CastInst::isLosslessCast() const {
1382 // Only BitCast can be lossless, exit fast if we're not BitCast
1383 if (getOpcode() != Instruction::BitCast)
1384 return false;
1385
1386 // Identity cast is always lossless
1387 const Type* SrcTy = getOperand(0)->getType();
1388 const Type* DstTy = getType();
1389 if (SrcTy == DstTy)
1390 return true;
1391
Reid Spencer8d9336d2006-12-31 05:26:44 +00001392 // Pointer to pointer is always lossless.
1393 if (isa<PointerType>(SrcTy))
1394 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001395 return false; // Other types have no identity values
1396}
1397
1398/// This function determines if the CastInst does not require any bits to be
1399/// changed in order to effect the cast. Essentially, it identifies cases where
1400/// no code gen is necessary for the cast, hence the name no-op cast. For
1401/// example, the following are all no-op casts:
1402/// # bitcast uint %X, int
1403/// # bitcast uint* %x, sbyte*
1404/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1405/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1406/// @brief Determine if a cast is a no-op.
1407bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1408 switch (getOpcode()) {
1409 default:
1410 assert(!"Invalid CastOp");
1411 case Instruction::Trunc:
1412 case Instruction::ZExt:
1413 case Instruction::SExt:
1414 case Instruction::FPTrunc:
1415 case Instruction::FPExt:
1416 case Instruction::UIToFP:
1417 case Instruction::SIToFP:
1418 case Instruction::FPToUI:
1419 case Instruction::FPToSI:
1420 return false; // These always modify bits
1421 case Instruction::BitCast:
1422 return true; // BitCast never modifies bits.
1423 case Instruction::PtrToInt:
1424 return IntPtrTy->getPrimitiveSizeInBits() ==
1425 getType()->getPrimitiveSizeInBits();
1426 case Instruction::IntToPtr:
1427 return IntPtrTy->getPrimitiveSizeInBits() ==
1428 getOperand(0)->getType()->getPrimitiveSizeInBits();
1429 }
1430}
1431
1432/// This function determines if a pair of casts can be eliminated and what
1433/// opcode should be used in the elimination. This assumes that there are two
1434/// instructions like this:
1435/// * %F = firstOpcode SrcTy %x to MidTy
1436/// * %S = secondOpcode MidTy %F to DstTy
1437/// The function returns a resultOpcode so these two casts can be replaced with:
1438/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1439/// If no such cast is permited, the function returns 0.
1440unsigned CastInst::isEliminableCastPair(
1441 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1442 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1443{
1444 // Define the 144 possibilities for these two cast instructions. The values
1445 // in this matrix determine what to do in a given situation and select the
1446 // case in the switch below. The rows correspond to firstOp, the columns
1447 // correspond to secondOp. In looking at the table below, keep in mind
1448 // the following cast properties:
1449 //
1450 // Size Compare Source Destination
1451 // Operator Src ? Size Type Sign Type Sign
1452 // -------- ------------ ------------------- ---------------------
1453 // TRUNC > Integer Any Integral Any
1454 // ZEXT < Integral Unsigned Integer Any
1455 // SEXT < Integral Signed Integer Any
1456 // FPTOUI n/a FloatPt n/a Integral Unsigned
1457 // FPTOSI n/a FloatPt n/a Integral Signed
1458 // UITOFP n/a Integral Unsigned FloatPt n/a
1459 // SITOFP n/a Integral Signed FloatPt n/a
1460 // FPTRUNC > FloatPt n/a FloatPt n/a
1461 // FPEXT < FloatPt n/a FloatPt n/a
1462 // PTRTOINT n/a Pointer n/a Integral Unsigned
1463 // INTTOPTR n/a Integral Unsigned Pointer n/a
1464 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001465 //
1466 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1467 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1468 // into "fptoui double to ulong", but this loses information about the range
1469 // of the produced value (we no longer know the top-part is all zeros).
1470 // Further this conversion is often much more expensive for typical hardware,
1471 // and causes issues when building libgcc. We disallow fptosi+sext for the
1472 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001473 const unsigned numCastOps =
1474 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1475 static const uint8_t CastResults[numCastOps][numCastOps] = {
1476 // T F F U S F F P I B -+
1477 // R Z S P P I I T P 2 N T |
1478 // U E E 2 2 2 2 R E I T C +- secondOp
1479 // N X X U S F F N X N 2 V |
1480 // C T T I I P P C T T P T -+
1481 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1482 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1483 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001484 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1485 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001486 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1487 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1488 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1489 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1490 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1491 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1492 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1493 };
1494
1495 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1496 [secondOp-Instruction::CastOpsBegin];
1497 switch (ElimCase) {
1498 case 0:
1499 // categorically disallowed
1500 return 0;
1501 case 1:
1502 // allowed, use first cast's opcode
1503 return firstOp;
1504 case 2:
1505 // allowed, use second cast's opcode
1506 return secondOp;
1507 case 3:
1508 // no-op cast in second op implies firstOp as long as the DestTy
1509 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001510 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001511 return firstOp;
1512 return 0;
1513 case 4:
1514 // no-op cast in second op implies firstOp as long as the DestTy
1515 // is floating point
1516 if (DstTy->isFloatingPoint())
1517 return firstOp;
1518 return 0;
1519 case 5:
1520 // no-op cast in first op implies secondOp as long as the SrcTy
1521 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001522 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001523 return secondOp;
1524 return 0;
1525 case 6:
1526 // no-op cast in first op implies secondOp as long as the SrcTy
1527 // is a floating point
1528 if (SrcTy->isFloatingPoint())
1529 return secondOp;
1530 return 0;
1531 case 7: {
1532 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1533 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1534 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1535 if (MidSize >= PtrSize)
1536 return Instruction::BitCast;
1537 return 0;
1538 }
1539 case 8: {
1540 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1541 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1542 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1543 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1544 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1545 if (SrcSize == DstSize)
1546 return Instruction::BitCast;
1547 else if (SrcSize < DstSize)
1548 return firstOp;
1549 return secondOp;
1550 }
1551 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1552 return Instruction::ZExt;
1553 case 10:
1554 // fpext followed by ftrunc is allowed if the bit size returned to is
1555 // the same as the original, in which case its just a bitcast
1556 if (SrcTy == DstTy)
1557 return Instruction::BitCast;
1558 return 0; // If the types are not the same we can't eliminate it.
1559 case 11:
1560 // bitcast followed by ptrtoint is allowed as long as the bitcast
1561 // is a pointer to pointer cast.
1562 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1563 return secondOp;
1564 return 0;
1565 case 12:
1566 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1567 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1568 return firstOp;
1569 return 0;
1570 case 13: {
1571 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1572 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1573 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1574 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1575 if (SrcSize <= PtrSize && SrcSize == DstSize)
1576 return Instruction::BitCast;
1577 return 0;
1578 }
1579 case 99:
1580 // cast combination can't happen (error in input). This is for all cases
1581 // where the MidTy is not the same for the two cast instructions.
1582 assert(!"Invalid Cast Combination");
1583 return 0;
1584 default:
1585 assert(!"Error in CastResults table!!!");
1586 return 0;
1587 }
1588 return 0;
1589}
1590
1591CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1592 const std::string &Name, Instruction *InsertBefore) {
1593 // Construct and return the appropriate CastInst subclass
1594 switch (op) {
1595 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1596 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1597 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1598 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1599 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1600 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1601 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1602 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1603 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1604 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1605 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1606 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1607 default:
1608 assert(!"Invalid opcode provided");
1609 }
1610 return 0;
1611}
1612
1613CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1614 const std::string &Name, BasicBlock *InsertAtEnd) {
1615 // Construct and return the appropriate CastInst subclass
1616 switch (op) {
1617 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1618 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1619 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1620 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1621 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1622 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1623 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1624 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1625 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1626 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1627 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1628 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1629 default:
1630 assert(!"Invalid opcode provided");
1631 }
1632 return 0;
1633}
1634
Reid Spencer5c140882006-12-04 20:17:56 +00001635CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1636 const std::string &Name,
1637 Instruction *InsertBefore) {
1638 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1639 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1640 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1641}
1642
1643CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1644 const std::string &Name,
1645 BasicBlock *InsertAtEnd) {
1646 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1647 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1648 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1649}
1650
1651CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1652 const std::string &Name,
1653 Instruction *InsertBefore) {
1654 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1655 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1656 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1657}
1658
1659CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1660 const std::string &Name,
1661 BasicBlock *InsertAtEnd) {
1662 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1663 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1664 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1665}
1666
1667CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1668 const std::string &Name,
1669 Instruction *InsertBefore) {
1670 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1671 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1672 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1673}
1674
1675CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1676 const std::string &Name,
1677 BasicBlock *InsertAtEnd) {
1678 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1679 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1680 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1681}
1682
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001683CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1684 const std::string &Name,
1685 BasicBlock *InsertAtEnd) {
1686 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001687 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001688 "Invalid cast");
1689
Chris Lattner03c49532007-01-15 02:27:26 +00001690 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001691 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1692 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1693}
1694
1695/// @brief Create a BitCast or a PtrToInt cast instruction
1696CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1697 const std::string &Name,
1698 Instruction *InsertBefore) {
1699 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001700 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001701 "Invalid cast");
1702
Chris Lattner03c49532007-01-15 02:27:26 +00001703 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001704 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1705 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1706}
1707
Reid Spencer7e933472006-12-12 00:49:44 +00001708CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1709 bool isSigned, const std::string &Name,
1710 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001711 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001712 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1713 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1714 Instruction::CastOps opcode =
1715 (SrcBits == DstBits ? Instruction::BitCast :
1716 (SrcBits > DstBits ? Instruction::Trunc :
1717 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1718 return create(opcode, C, Ty, Name, InsertBefore);
1719}
1720
1721CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1722 bool isSigned, const std::string &Name,
1723 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001724 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001725 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1726 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1727 Instruction::CastOps opcode =
1728 (SrcBits == DstBits ? Instruction::BitCast :
1729 (SrcBits > DstBits ? Instruction::Trunc :
1730 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1731 return create(opcode, C, Ty, Name, InsertAtEnd);
1732}
1733
1734CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1735 const std::string &Name,
1736 Instruction *InsertBefore) {
1737 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1738 "Invalid cast");
1739 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1740 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1741 Instruction::CastOps opcode =
1742 (SrcBits == DstBits ? Instruction::BitCast :
1743 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1744 return create(opcode, C, Ty, Name, InsertBefore);
1745}
1746
1747CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1748 const std::string &Name,
1749 BasicBlock *InsertAtEnd) {
1750 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1751 "Invalid cast");
1752 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1753 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1754 Instruction::CastOps opcode =
1755 (SrcBits == DstBits ? Instruction::BitCast :
1756 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1757 return create(opcode, C, Ty, Name, InsertAtEnd);
1758}
1759
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001760// Provide a way to get a "cast" where the cast opcode is inferred from the
1761// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001762// logic in the castIsValid function below. This axiom should hold:
1763// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1764// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001765// casting opcode for the arguments passed to it.
1766Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001767CastInst::getCastOpcode(
1768 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001769 // Get the bit sizes, we'll need these
1770 const Type *SrcTy = Src->getType();
1771 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1772 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1773
1774 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001775 if (DestTy->isInteger()) { // Casting to integral
1776 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001777 if (DestBits < SrcBits)
1778 return Trunc; // int -> smaller int
1779 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001780 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001781 return SExt; // signed -> SEXT
1782 else
1783 return ZExt; // unsigned -> ZEXT
1784 } else {
1785 return BitCast; // Same size, No-op cast
1786 }
1787 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001788 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001789 return FPToSI; // FP -> sint
1790 else
1791 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001792 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001793 assert(DestBits == PTy->getBitWidth() &&
1794 "Casting packed to integer of different width");
1795 return BitCast; // Same size, no-op cast
1796 } else {
1797 assert(isa<PointerType>(SrcTy) &&
1798 "Casting from a value that is not first-class type");
1799 return PtrToInt; // ptr -> int
1800 }
1801 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001802 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001803 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001804 return SIToFP; // sint -> FP
1805 else
1806 return UIToFP; // uint -> FP
1807 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1808 if (DestBits < SrcBits) {
1809 return FPTrunc; // FP -> smaller FP
1810 } else if (DestBits > SrcBits) {
1811 return FPExt; // FP -> larger FP
1812 } else {
1813 return BitCast; // same size, no-op cast
1814 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001815 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001816 assert(DestBits == PTy->getBitWidth() &&
1817 "Casting packed to floating point of different width");
1818 return BitCast; // same size, no-op cast
1819 } else {
1820 assert(0 && "Casting pointer or non-first class to float");
1821 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001822 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1823 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001824 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1825 "Casting packed to packed of different widths");
1826 return BitCast; // packed -> packed
1827 } else if (DestPTy->getBitWidth() == SrcBits) {
1828 return BitCast; // float/int -> packed
1829 } else {
1830 assert(!"Illegal cast to packed (wrong type or size)");
1831 }
1832 } else if (isa<PointerType>(DestTy)) {
1833 if (isa<PointerType>(SrcTy)) {
1834 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001835 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001836 return IntToPtr; // int -> ptr
1837 } else {
1838 assert(!"Casting pointer to other than pointer or int");
1839 }
1840 } else {
1841 assert(!"Casting to type that is not first-class");
1842 }
1843
1844 // If we fall through to here we probably hit an assertion cast above
1845 // and assertions are not turned on. Anything we return is an error, so
1846 // BitCast is as good a choice as any.
1847 return BitCast;
1848}
1849
1850//===----------------------------------------------------------------------===//
1851// CastInst SubClass Constructors
1852//===----------------------------------------------------------------------===//
1853
1854/// Check that the construction parameters for a CastInst are correct. This
1855/// could be broken out into the separate constructors but it is useful to have
1856/// it in one place and to eliminate the redundant code for getting the sizes
1857/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001858bool
1859CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001860
1861 // Check for type sanity on the arguments
1862 const Type *SrcTy = S->getType();
1863 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1864 return false;
1865
1866 // Get the size of the types in bits, we'll need this later
1867 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1868 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1869
1870 // Switch on the opcode provided
1871 switch (op) {
1872 default: return false; // This is an input error
1873 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001874 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001876 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001877 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001878 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001879 case Instruction::FPTrunc:
1880 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1881 SrcBitSize > DstBitSize;
1882 case Instruction::FPExt:
1883 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1884 SrcBitSize < DstBitSize;
1885 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001886 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001888 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001890 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001891 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001892 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001894 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001895 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001896 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001897 case Instruction::BitCast:
1898 // BitCast implies a no-op cast of type only. No bits change.
1899 // However, you can't cast pointers to anything but pointers.
1900 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1901 return false;
1902
1903 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1904 // these cases, the cast is okay if the source and destination bit widths
1905 // are identical.
1906 return SrcBitSize == DstBitSize;
1907 }
1908}
1909
1910TruncInst::TruncInst(
1911 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1912) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001913 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001914}
1915
1916TruncInst::TruncInst(
1917 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1918) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001919 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001920}
1921
1922ZExtInst::ZExtInst(
1923 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1924) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001925 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001926}
1927
1928ZExtInst::ZExtInst(
1929 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1930) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001931 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001932}
1933SExtInst::SExtInst(
1934 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1935) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001936 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001937}
1938
Jeff Cohencc08c832006-12-02 02:22:01 +00001939SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001940 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1941) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001942 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001943}
1944
1945FPTruncInst::FPTruncInst(
1946 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1947) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001948 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001949}
1950
1951FPTruncInst::FPTruncInst(
1952 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1953) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001954 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955}
1956
1957FPExtInst::FPExtInst(
1958 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1959) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001960 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961}
1962
1963FPExtInst::FPExtInst(
1964 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1965) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001966 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967}
1968
1969UIToFPInst::UIToFPInst(
1970 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1971) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001972 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973}
1974
1975UIToFPInst::UIToFPInst(
1976 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1977) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001978 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001979}
1980
1981SIToFPInst::SIToFPInst(
1982 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1983) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001984 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001985}
1986
1987SIToFPInst::SIToFPInst(
1988 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1989) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001990 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991}
1992
1993FPToUIInst::FPToUIInst(
1994 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1995) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001996 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001997}
1998
1999FPToUIInst::FPToUIInst(
2000 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2001) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002002 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003}
2004
2005FPToSIInst::FPToSIInst(
2006 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2007) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002008 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009}
2010
2011FPToSIInst::FPToSIInst(
2012 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2013) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002014 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015}
2016
2017PtrToIntInst::PtrToIntInst(
2018 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2019) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002020 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021}
2022
2023PtrToIntInst::PtrToIntInst(
2024 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2025) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002026 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002027}
2028
2029IntToPtrInst::IntToPtrInst(
2030 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2031) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002032 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033}
2034
2035IntToPtrInst::IntToPtrInst(
2036 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2037) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002038 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002039}
2040
2041BitCastInst::BitCastInst(
2042 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2043) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002044 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045}
2046
2047BitCastInst::BitCastInst(
2048 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2049) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002050 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002051}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002052
2053//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002054// CmpInst Classes
2055//===----------------------------------------------------------------------===//
2056
2057CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2058 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002059 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002060 Ops[0].init(LHS, this);
2061 Ops[1].init(RHS, this);
2062 SubclassData = predicate;
2063 if (op == Instruction::ICmp) {
2064 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2065 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2066 "Invalid ICmp predicate value");
2067 const Type* Op0Ty = getOperand(0)->getType();
2068 const Type* Op1Ty = getOperand(1)->getType();
2069 assert(Op0Ty == Op1Ty &&
2070 "Both operands to ICmp instruction are not of the same type!");
2071 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002072 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002073 "Invalid operand types for ICmp instruction");
2074 return;
2075 }
2076 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2077 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2078 "Invalid FCmp predicate value");
2079 const Type* Op0Ty = getOperand(0)->getType();
2080 const Type* Op1Ty = getOperand(1)->getType();
2081 assert(Op0Ty == Op1Ty &&
2082 "Both operands to FCmp instruction are not of the same type!");
2083 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002084 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002085 "Invalid operand types for FCmp instruction");
Chris Lattner2195fc42007-02-24 00:55:48 +00002086 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002087}
2088
2089CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2090 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002091 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002092 Ops[0].init(LHS, this);
2093 Ops[1].init(RHS, this);
2094 SubclassData = predicate;
2095 if (op == Instruction::ICmp) {
2096 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2097 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2098 "Invalid ICmp predicate value");
2099
2100 const Type* Op0Ty = getOperand(0)->getType();
2101 const Type* Op1Ty = getOperand(1)->getType();
2102 assert(Op0Ty == Op1Ty &&
2103 "Both operands to ICmp instruction are not of the same type!");
2104 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002105 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002106 "Invalid operand types for ICmp instruction");
2107 return;
2108 }
2109 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2110 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2111 "Invalid FCmp predicate value");
2112 const Type* Op0Ty = getOperand(0)->getType();
2113 const Type* Op1Ty = getOperand(1)->getType();
2114 assert(Op0Ty == Op1Ty &&
2115 "Both operands to FCmp instruction are not of the same type!");
2116 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002117 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002118 "Invalid operand types for FCmp instruction");
Chris Lattner2195fc42007-02-24 00:55:48 +00002119 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002120}
2121
2122CmpInst *
2123CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2124 const std::string &Name, Instruction *InsertBefore) {
2125 if (Op == Instruction::ICmp) {
2126 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2127 InsertBefore);
2128 }
2129 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2130 InsertBefore);
2131}
2132
2133CmpInst *
2134CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2135 const std::string &Name, BasicBlock *InsertAtEnd) {
2136 if (Op == Instruction::ICmp) {
2137 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2138 InsertAtEnd);
2139 }
2140 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2141 InsertAtEnd);
2142}
2143
2144void CmpInst::swapOperands() {
2145 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2146 IC->swapOperands();
2147 else
2148 cast<FCmpInst>(this)->swapOperands();
2149}
2150
2151bool CmpInst::isCommutative() {
2152 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2153 return IC->isCommutative();
2154 return cast<FCmpInst>(this)->isCommutative();
2155}
2156
2157bool CmpInst::isEquality() {
2158 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2159 return IC->isEquality();
2160 return cast<FCmpInst>(this)->isEquality();
2161}
2162
2163
2164ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2165 switch (pred) {
2166 default:
2167 assert(!"Unknown icmp predicate!");
2168 case ICMP_EQ: return ICMP_NE;
2169 case ICMP_NE: return ICMP_EQ;
2170 case ICMP_UGT: return ICMP_ULE;
2171 case ICMP_ULT: return ICMP_UGE;
2172 case ICMP_UGE: return ICMP_ULT;
2173 case ICMP_ULE: return ICMP_UGT;
2174 case ICMP_SGT: return ICMP_SLE;
2175 case ICMP_SLT: return ICMP_SGE;
2176 case ICMP_SGE: return ICMP_SLT;
2177 case ICMP_SLE: return ICMP_SGT;
2178 }
2179}
2180
2181ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2182 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002183 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002184 case ICMP_EQ: case ICMP_NE:
2185 return pred;
2186 case ICMP_SGT: return ICMP_SLT;
2187 case ICMP_SLT: return ICMP_SGT;
2188 case ICMP_SGE: return ICMP_SLE;
2189 case ICMP_SLE: return ICMP_SGE;
2190 case ICMP_UGT: return ICMP_ULT;
2191 case ICMP_ULT: return ICMP_UGT;
2192 case ICMP_UGE: return ICMP_ULE;
2193 case ICMP_ULE: return ICMP_UGE;
2194 }
2195}
2196
Reid Spencer266e42b2006-12-23 06:05:41 +00002197ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2198 switch (pred) {
2199 default: assert(! "Unknown icmp predicate!");
2200 case ICMP_EQ: case ICMP_NE:
2201 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2202 return pred;
2203 case ICMP_UGT: return ICMP_SGT;
2204 case ICMP_ULT: return ICMP_SLT;
2205 case ICMP_UGE: return ICMP_SGE;
2206 case ICMP_ULE: return ICMP_SLE;
2207 }
2208}
2209
2210bool ICmpInst::isSignedPredicate(Predicate pred) {
2211 switch (pred) {
2212 default: assert(! "Unknown icmp predicate!");
2213 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2214 return true;
2215 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2216 case ICMP_UGE: case ICMP_ULE:
2217 return false;
2218 }
2219}
2220
Reid Spencer0286bc12007-02-28 22:00:54 +00002221/// Initialize a set of values that all satisfy the condition with C.
2222///
2223ConstantRange
2224ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2225 APInt Lower(C);
2226 APInt Upper(C);
2227 uint32_t BitWidth = C.getBitWidth();
2228 switch (pred) {
2229 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2230 case ICmpInst::ICMP_EQ: Upper++; break;
2231 case ICmpInst::ICMP_NE: Lower++; break;
2232 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2233 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2234 case ICmpInst::ICMP_UGT:
2235 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2236 break;
2237 case ICmpInst::ICMP_SGT:
2238 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2239 break;
2240 case ICmpInst::ICMP_ULE:
2241 Lower = APInt::getMinValue(BitWidth); Upper++;
2242 break;
2243 case ICmpInst::ICMP_SLE:
2244 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2245 break;
2246 case ICmpInst::ICMP_UGE:
2247 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2248 break;
2249 case ICmpInst::ICMP_SGE:
2250 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2251 break;
2252 }
2253 return ConstantRange(Lower, Upper);
2254}
2255
Reid Spencerd9436b62006-11-20 01:22:35 +00002256FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2257 switch (pred) {
2258 default:
2259 assert(!"Unknown icmp predicate!");
2260 case FCMP_OEQ: return FCMP_UNE;
2261 case FCMP_ONE: return FCMP_UEQ;
2262 case FCMP_OGT: return FCMP_ULE;
2263 case FCMP_OLT: return FCMP_UGE;
2264 case FCMP_OGE: return FCMP_ULT;
2265 case FCMP_OLE: return FCMP_UGT;
2266 case FCMP_UEQ: return FCMP_ONE;
2267 case FCMP_UNE: return FCMP_OEQ;
2268 case FCMP_UGT: return FCMP_OLE;
2269 case FCMP_ULT: return FCMP_OGE;
2270 case FCMP_UGE: return FCMP_OLT;
2271 case FCMP_ULE: return FCMP_OGT;
2272 case FCMP_ORD: return FCMP_UNO;
2273 case FCMP_UNO: return FCMP_ORD;
2274 case FCMP_TRUE: return FCMP_FALSE;
2275 case FCMP_FALSE: return FCMP_TRUE;
2276 }
2277}
2278
2279FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2280 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002281 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002282 case FCMP_FALSE: case FCMP_TRUE:
2283 case FCMP_OEQ: case FCMP_ONE:
2284 case FCMP_UEQ: case FCMP_UNE:
2285 case FCMP_ORD: case FCMP_UNO:
2286 return pred;
2287 case FCMP_OGT: return FCMP_OLT;
2288 case FCMP_OLT: return FCMP_OGT;
2289 case FCMP_OGE: return FCMP_OLE;
2290 case FCMP_OLE: return FCMP_OGE;
2291 case FCMP_UGT: return FCMP_ULT;
2292 case FCMP_ULT: return FCMP_UGT;
2293 case FCMP_UGE: return FCMP_ULE;
2294 case FCMP_ULE: return FCMP_UGE;
2295 }
2296}
2297
Reid Spencer266e42b2006-12-23 06:05:41 +00002298bool CmpInst::isUnsigned(unsigned short predicate) {
2299 switch (predicate) {
2300 default: return false;
2301 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2302 case ICmpInst::ICMP_UGE: return true;
2303 }
2304}
2305
2306bool CmpInst::isSigned(unsigned short predicate){
2307 switch (predicate) {
2308 default: return false;
2309 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2310 case ICmpInst::ICMP_SGE: return true;
2311 }
2312}
2313
2314bool CmpInst::isOrdered(unsigned short predicate) {
2315 switch (predicate) {
2316 default: return false;
2317 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2318 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2319 case FCmpInst::FCMP_ORD: return true;
2320 }
2321}
2322
2323bool CmpInst::isUnordered(unsigned short predicate) {
2324 switch (predicate) {
2325 default: return false;
2326 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2327 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2328 case FCmpInst::FCMP_UNO: return true;
2329 }
2330}
2331
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002332//===----------------------------------------------------------------------===//
2333// SwitchInst Implementation
2334//===----------------------------------------------------------------------===//
2335
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002336void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002337 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002338 ReservedSpace = 2+NumCases*2;
2339 NumOperands = 2;
2340 OperandList = new Use[ReservedSpace];
2341
2342 OperandList[0].init(Value, this);
2343 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002344}
2345
Chris Lattner2195fc42007-02-24 00:55:48 +00002346/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2347/// switch on and a default destination. The number of additional cases can
2348/// be specified here to make memory allocation more efficient. This
2349/// constructor can also autoinsert before another instruction.
2350SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2351 Instruction *InsertBefore)
2352 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2353 init(Value, Default, NumCases);
2354}
2355
2356/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2357/// switch on and a default destination. The number of additional cases can
2358/// be specified here to make memory allocation more efficient. This
2359/// constructor also autoinserts at the end of the specified BasicBlock.
2360SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2361 BasicBlock *InsertAtEnd)
2362 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2363 init(Value, Default, NumCases);
2364}
2365
Misha Brukmanb1c93172005-04-21 23:48:37 +00002366SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002367 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2368 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002369 Use *OL = OperandList, *InOL = SI.OperandList;
2370 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2371 OL[i].init(InOL[i], this);
2372 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002373 }
2374}
2375
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002376SwitchInst::~SwitchInst() {
2377 delete [] OperandList;
2378}
2379
2380
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002381/// addCase - Add an entry to the switch instruction...
2382///
Chris Lattner47ac1872005-02-24 05:32:09 +00002383void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002384 unsigned OpNo = NumOperands;
2385 if (OpNo+2 > ReservedSpace)
2386 resizeOperands(0); // Get more space!
2387 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002388 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002389 NumOperands = OpNo+2;
2390 OperandList[OpNo].init(OnVal, this);
2391 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002392}
2393
2394/// removeCase - This method removes the specified successor from the switch
2395/// instruction. Note that this cannot be used to remove the default
2396/// destination (successor #0).
2397///
2398void SwitchInst::removeCase(unsigned idx) {
2399 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002400 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2401
2402 unsigned NumOps = getNumOperands();
2403 Use *OL = OperandList;
2404
2405 // Move everything after this operand down.
2406 //
2407 // FIXME: we could just swap with the end of the list, then erase. However,
2408 // client might not expect this to happen. The code as it is thrashes the
2409 // use/def lists, which is kinda lame.
2410 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2411 OL[i-2] = OL[i];
2412 OL[i-2+1] = OL[i+1];
2413 }
2414
2415 // Nuke the last value.
2416 OL[NumOps-2].set(0);
2417 OL[NumOps-2+1].set(0);
2418 NumOperands = NumOps-2;
2419}
2420
2421/// resizeOperands - resize operands - This adjusts the length of the operands
2422/// list according to the following behavior:
2423/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2424/// of operation. This grows the number of ops by 1.5 times.
2425/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2426/// 3. If NumOps == NumOperands, trim the reserved space.
2427///
2428void SwitchInst::resizeOperands(unsigned NumOps) {
2429 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002430 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002431 } else if (NumOps*2 > NumOperands) {
2432 // No resize needed.
2433 if (ReservedSpace >= NumOps) return;
2434 } else if (NumOps == NumOperands) {
2435 if (ReservedSpace == NumOps) return;
2436 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002437 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002438 }
2439
2440 ReservedSpace = NumOps;
2441 Use *NewOps = new Use[NumOps];
2442 Use *OldOps = OperandList;
2443 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2444 NewOps[i].init(OldOps[i], this);
2445 OldOps[i].set(0);
2446 }
2447 delete [] OldOps;
2448 OperandList = NewOps;
2449}
2450
2451
2452BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2453 return getSuccessor(idx);
2454}
2455unsigned SwitchInst::getNumSuccessorsV() const {
2456 return getNumSuccessors();
2457}
2458void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2459 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002460}
Chris Lattnerf22be932004-10-15 23:52:53 +00002461
2462
2463// Define these methods here so vtables don't get emitted into every translation
2464// unit that uses these classes.
2465
2466GetElementPtrInst *GetElementPtrInst::clone() const {
2467 return new GetElementPtrInst(*this);
2468}
2469
2470BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002471 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002472}
2473
Reid Spencerd9436b62006-11-20 01:22:35 +00002474CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002475 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002476}
2477
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2479AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2480FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2481LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2482StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2483CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2484CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2485CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2486CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2487CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2488CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2489CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2490CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2491CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2492CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2493CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2494CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2495CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2497VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2498
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002499ExtractElementInst *ExtractElementInst::clone() const {
2500 return new ExtractElementInst(*this);
2501}
2502InsertElementInst *InsertElementInst::clone() const {
2503 return new InsertElementInst(*this);
2504}
2505ShuffleVectorInst *ShuffleVectorInst::clone() const {
2506 return new ShuffleVectorInst(*this);
2507}
Chris Lattnerf22be932004-10-15 23:52:53 +00002508PHINode *PHINode::clone() const { return new PHINode(*this); }
2509ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2510BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2511SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2512InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2513UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002514UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}