blob: e591845e40382ac3cf8890821f71db1c699ae629 [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"
21using namespace llvm;
22
Chris Lattnerf7b6d312005-05-06 20:26:43 +000023unsigned CallSite::getCallingConv() const {
24 if (CallInst *CI = dyn_cast<CallInst>(I))
25 return CI->getCallingConv();
26 else
27 return cast<InvokeInst>(I)->getCallingConv();
28}
29void CallSite::setCallingConv(unsigned CC) {
30 if (CallInst *CI = dyn_cast<CallInst>(I))
31 CI->setCallingConv(CC);
32 else
33 cast<InvokeInst>(I)->setCallingConv(CC);
34}
35
36
Chris Lattner1c12a882006-06-21 16:53:47 +000037
38
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000039//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000040// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43TerminatorInst::TerminatorInst(Instruction::TermOps iType,
Misha Brukmanb1c93172005-04-21 23:48:37 +000044 Use *Ops, unsigned NumOps, Instruction *IB)
Chris Lattnerafdb3de2005-01-29 00:35:16 +000045 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
46}
47
48TerminatorInst::TerminatorInst(Instruction::TermOps iType,
49 Use *Ops, unsigned NumOps, BasicBlock *IAE)
50 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
51}
52
Chris Lattner1c12a882006-06-21 16:53:47 +000053// Out of line virtual method, so the vtable, etc has a home.
54TerminatorInst::~TerminatorInst() {
55}
56
57// Out of line virtual method, so the vtable, etc has a home.
58UnaryInstruction::~UnaryInstruction() {
59}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000060
61
62//===----------------------------------------------------------------------===//
63// PHINode Class
64//===----------------------------------------------------------------------===//
65
66PHINode::PHINode(const PHINode &PN)
67 : Instruction(PN.getType(), Instruction::PHI,
68 new Use[PN.getNumOperands()], PN.getNumOperands()),
69 ReservedSpace(PN.getNumOperands()) {
70 Use *OL = OperandList;
71 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
72 OL[i].init(PN.getOperand(i), this);
73 OL[i+1].init(PN.getOperand(i+1), this);
74 }
75}
76
77PHINode::~PHINode() {
78 delete [] OperandList;
79}
80
81// removeIncomingValue - Remove an incoming value. This is useful if a
82// predecessor basic block is deleted.
83Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
84 unsigned NumOps = getNumOperands();
85 Use *OL = OperandList;
86 assert(Idx*2 < NumOps && "BB not in PHI node!");
87 Value *Removed = OL[Idx*2];
88
89 // Move everything after this operand down.
90 //
91 // FIXME: we could just swap with the end of the list, then erase. However,
92 // client might not expect this to happen. The code as it is thrashes the
93 // use/def lists, which is kinda lame.
94 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
95 OL[i-2] = OL[i];
96 OL[i-2+1] = OL[i+1];
97 }
98
99 // Nuke the last value.
100 OL[NumOps-2].set(0);
101 OL[NumOps-2+1].set(0);
102 NumOperands = NumOps-2;
103
104 // If the PHI node is dead, because it has zero entries, nuke it now.
105 if (NumOps == 2 && DeletePHIIfEmpty) {
106 // If anyone is using this PHI, make them use a dummy value instead...
107 replaceAllUsesWith(UndefValue::get(getType()));
108 eraseFromParent();
109 }
110 return Removed;
111}
112
113/// resizeOperands - resize operands - This adjusts the length of the operands
114/// list according to the following behavior:
115/// 1. If NumOps == 0, grow the operand list in response to a push_back style
116/// of operation. This grows the number of ops by 1.5 times.
117/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
118/// 3. If NumOps == NumOperands, trim the reserved space.
119///
120void PHINode::resizeOperands(unsigned NumOps) {
121 if (NumOps == 0) {
122 NumOps = (getNumOperands())*3/2;
123 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
124 } else if (NumOps*2 > NumOperands) {
125 // No resize needed.
126 if (ReservedSpace >= NumOps) return;
127 } else if (NumOps == NumOperands) {
128 if (ReservedSpace == NumOps) return;
129 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 }
132
133 ReservedSpace = NumOps;
134 Use *NewOps = new Use[NumOps];
135 Use *OldOps = OperandList;
136 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
137 NewOps[i].init(OldOps[i], this);
138 OldOps[i].set(0);
139 }
140 delete [] OldOps;
141 OperandList = NewOps;
142}
143
Nate Begemanb3923212005-08-04 23:24:19 +0000144/// hasConstantValue - If the specified PHI node always merges together the same
145/// value, return the value, otherwise return null.
146///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000147Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000148 // If the PHI node only has one incoming value, eliminate the PHI node...
149 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000150 if (getIncomingValue(0) != this) // not X = phi X
151 return getIncomingValue(0);
152 else
153 return UndefValue::get(getType()); // Self cycle is dead.
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155 // Otherwise if all of the incoming values are the same for the PHI, replace
156 // the PHI node with the incoming value.
157 //
158 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000159 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000160 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000161 if (isa<UndefValue>(getIncomingValue(i)))
162 HasUndefInput = true;
163 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000164 if (InVal && getIncomingValue(i) != InVal)
165 return 0; // Not the same, bail out.
166 else
167 InVal = getIncomingValue(i);
168
169 // The only case that could cause InVal to be null is if we have a PHI node
170 // that only has entries for itself. In this case, there is no entry into the
171 // loop, so kill the PHI.
172 //
173 if (InVal == 0) InVal = UndefValue::get(getType());
174
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 // If we have a PHI node like phi(X, undef, X), where X is defined by some
176 // instruction, we cannot always return X as the result of the PHI node. Only
177 // do this if X is not an instruction (thus it must dominate the PHI block),
178 // or if the client is prepared to deal with this possibility.
179 if (HasUndefInput && !AllowNonDominatingInstruction)
180 if (Instruction *IV = dyn_cast<Instruction>(InVal))
181 // If it's in the entry block, it dominates everything.
Chris Lattner37774af2005-08-05 01:03:27 +0000182 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
183 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000184 return 0; // Cannot guarantee that InVal dominates this PHINode.
185
Nate Begemanb3923212005-08-04 23:24:19 +0000186 // All of the incoming values are the same, return the value now.
187 return InVal;
188}
189
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190
191//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000192// CallInst Implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195CallInst::~CallInst() {
196 delete [] OperandList;
197}
198
Chris Lattner054ba2c2007-02-13 00:58:44 +0000199void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
200 NumOperands = NumParams+1;
201 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203
Misha Brukmanb1c93172005-04-21 23:48:37 +0000204 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000205 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000206 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000207
Chris Lattner054ba2c2007-02-13 00:58:44 +0000208 assert((NumParams == FTy->getNumParams() ||
209 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000210 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000211 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000212 assert((i >= FTy->getNumParams() ||
213 FTy->getParamType(i) == Params[i]->getType()) &&
214 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000216 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000217}
218
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
220 NumOperands = 3;
221 Use *OL = OperandList = new Use[3];
222 OL[0].init(Func, this);
223 OL[1].init(Actual1, this);
224 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000225
226 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000227 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000228 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000231 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000233 assert((0 >= FTy->getNumParams() ||
234 FTy->getParamType(0) == Actual1->getType()) &&
235 "Calling a function with a bad signature!");
236 assert((1 >= FTy->getNumParams() ||
237 FTy->getParamType(1) == Actual2->getType()) &&
238 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239}
240
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241void CallInst::init(Value *Func, Value *Actual) {
242 NumOperands = 2;
243 Use *OL = OperandList = new Use[2];
244 OL[0].init(Func, this);
245 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000246
247 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000249 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000251 assert((FTy->getNumParams() == 1 ||
252 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000254 assert((0 == FTy->getNumParams() ||
255 FTy->getParamType(0) == Actual->getType()) &&
256 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257}
258
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000259void CallInst::init(Value *Func) {
260 NumOperands = 1;
261 Use *OL = OperandList = new Use[1];
262 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000263
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000264 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000266 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000268 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269}
270
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000271CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000272 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
274 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000275 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000276 init(Func, Args, NumArgs);
277}
278CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
279 const std::string &Name, Instruction *InsertBefore)
280: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
281 ->getElementType())->getReturnType(),
282 Instruction::Call, 0, 0, Name, InsertBefore) {
283 init(Func, Args, NumArgs);
284}
285
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
287 const std::string &Name, Instruction *InsertBefore)
288 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
289 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000290 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291 init(Func, Actual1, Actual2);
292}
293
294CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
295 const std::string &Name, BasicBlock *InsertAtEnd)
296 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
297 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000298 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299 init(Func, Actual1, Actual2);
300}
301
302CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
303 Instruction *InsertBefore)
304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000306 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 init(Func, Actual);
308}
309
310CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
311 BasicBlock *InsertAtEnd)
312 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
313 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000314 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 init(Func, Actual);
316}
317
318CallInst::CallInst(Value *Func, const std::string &Name,
319 Instruction *InsertBefore)
320 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
321 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000322 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 init(Func);
324}
325
326CallInst::CallInst(Value *Func, const std::string &Name,
327 BasicBlock *InsertAtEnd)
328 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
329 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000330 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331 init(Func);
332}
333
Misha Brukmanb1c93172005-04-21 23:48:37 +0000334CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000335 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
336 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000337 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338 Use *OL = OperandList;
339 Use *InOL = CI.OperandList;
340 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
341 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342}
343
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344
345//===----------------------------------------------------------------------===//
346// InvokeInst Implementation
347//===----------------------------------------------------------------------===//
348
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000349InvokeInst::~InvokeInst() {
350 delete [] OperandList;
351}
352
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000354 Value* const *Args, unsigned NumArgs) {
355 NumOperands = 3+NumArgs;
356 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000357 OL[0].init(Fn, this);
358 OL[1].init(IfNormal, this);
359 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000360 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000362 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000363
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000364 assert((NumArgs == FTy->getNumParams()) ||
365 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000366 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000367
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000368 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000369 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000370 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000371 "Invoking a function with a bad signature!");
372
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000373 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000374 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375}
376
377InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
378 BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000379 Value* const *Args, unsigned NumArgs,
380 const std::string &Name, Instruction *InsertBefore)
381 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
382 ->getElementType())->getReturnType(),
383 Instruction::Invoke, 0, 0, Name, InsertBefore) {
384 init(Fn, IfNormal, IfException, Args, NumArgs);
385}
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(),
393 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
394 init(Fn, IfNormal, IfException, Args, NumArgs);
395}
396
Misha Brukmanb1c93172005-04-21 23:48:37 +0000397InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000398 : TerminatorInst(II.getType(), Instruction::Invoke,
399 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000400 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000401 Use *OL = OperandList, *InOL = II.OperandList;
402 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
403 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000404}
405
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000406BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
407 return getSuccessor(idx);
408}
409unsigned InvokeInst::getNumSuccessorsV() const {
410 return getNumSuccessors();
411}
412void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
413 return setSuccessor(idx, B);
414}
415
416
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000417//===----------------------------------------------------------------------===//
418// ReturnInst Implementation
419//===----------------------------------------------------------------------===//
420
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000421void ReturnInst::init(Value *retVal) {
422 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000423 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000424 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000425 NumOperands = 1;
426 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000427 }
428}
429
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000430unsigned ReturnInst::getNumSuccessorsV() const {
431 return getNumSuccessors();
432}
433
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000434// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
435// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000436void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000437 assert(0 && "ReturnInst has no successors!");
438}
439
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000440BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
441 assert(0 && "ReturnInst has no successors!");
442 abort();
443 return 0;
444}
445
446
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000447//===----------------------------------------------------------------------===//
448// UnwindInst Implementation
449//===----------------------------------------------------------------------===//
450
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000451unsigned UnwindInst::getNumSuccessorsV() const {
452 return getNumSuccessors();
453}
454
455void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000456 assert(0 && "UnwindInst has no successors!");
457}
458
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000459BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
460 assert(0 && "UnwindInst has no successors!");
461 abort();
462 return 0;
463}
464
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000465//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000466// UnreachableInst Implementation
467//===----------------------------------------------------------------------===//
468
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000469unsigned UnreachableInst::getNumSuccessorsV() const {
470 return getNumSuccessors();
471}
472
473void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
474 assert(0 && "UnwindInst has no successors!");
475}
476
477BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
478 assert(0 && "UnwindInst has no successors!");
479 abort();
480 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000481}
482
483//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000484// BranchInst Implementation
485//===----------------------------------------------------------------------===//
486
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000487void BranchInst::AssertOK() {
488 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000489 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000490 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000491}
492
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000493BranchInst::BranchInst(const BranchInst &BI) :
494 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
495 OperandList[0].init(BI.getOperand(0), this);
496 if (BI.getNumOperands() != 1) {
497 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
498 OperandList[1].init(BI.getOperand(1), this);
499 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000500 }
501}
502
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000503BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
504 return getSuccessor(idx);
505}
506unsigned BranchInst::getNumSuccessorsV() const {
507 return getNumSuccessors();
508}
509void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
510 setSuccessor(idx, B);
511}
512
513
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000514//===----------------------------------------------------------------------===//
515// AllocationInst Implementation
516//===----------------------------------------------------------------------===//
517
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000518static Value *getAISize(Value *Amt) {
519 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000520 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000521 else {
522 assert(!isa<BasicBlock>(Amt) &&
523 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000524 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000525 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000526 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000527 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528}
529
Misha Brukmanb1c93172005-04-21 23:48:37 +0000530AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000531 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000532 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000533 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner0f048162007-02-13 07:54:42 +0000534 0, InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000535 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000536 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000537 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538}
539
Misha Brukmanb1c93172005-04-21 23:48:37 +0000540AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000541 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner0f048162007-02-13 07:54:42 +0000544 0, InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000545 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000546 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000547 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000548}
549
Chris Lattner1c12a882006-06-21 16:53:47 +0000550// Out of line virtual method, so the vtable, etc has a home.
551AllocationInst::~AllocationInst() {
552}
553
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000554bool AllocationInst::isArrayAllocation() const {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000555 if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0)))
556 return CUI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000557 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000558}
559
560const Type *AllocationInst::getAllocatedType() const {
561 return getType()->getElementType();
562}
563
564AllocaInst::AllocaInst(const AllocaInst &AI)
565 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000566 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000567}
568
569MallocInst::MallocInst(const MallocInst &MI)
570 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000571 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572}
573
574//===----------------------------------------------------------------------===//
575// FreeInst Implementation
576//===----------------------------------------------------------------------===//
577
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578void FreeInst::AssertOK() {
579 assert(isa<PointerType>(getOperand(0)->getType()) &&
580 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581}
582
583FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner0f048162007-02-13 07:54:42 +0000584 : UnaryInstruction(Type::VoidTy, Free, Ptr, 0, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000585 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000586}
587
588FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner0f048162007-02-13 07:54:42 +0000589 : UnaryInstruction(Type::VoidTy, Free, Ptr, 0, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000591}
592
593
594//===----------------------------------------------------------------------===//
595// LoadInst Implementation
596//===----------------------------------------------------------------------===//
597
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000598void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000599 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000600 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000601}
602
603LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000604 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner0f048162007-02-13 07:54:42 +0000605 Load, Ptr, 0, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000606 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000608 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609}
610
611LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000612 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner0f048162007-02-13 07:54:42 +0000613 Load, Ptr, 0, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000614 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000615 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000616 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000617}
618
619LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
620 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000621 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner0f048162007-02-13 07:54:42 +0000622 Load, Ptr, 0, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000623 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000625 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000626}
627
628LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
629 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000630 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner0f048162007-02-13 07:54:42 +0000631 Load, Ptr, 0, InsertAE) {
632 setVolatile(isVolatile);
633 AssertOK();
634 setName(Name);
635}
636
637
638
639LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
640: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
641 Load, Ptr, Name, InsertBef) {
642 setVolatile(false);
643 AssertOK();
644}
645
646LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
647: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
648 Load, Ptr, Name, InsertAE) {
649 setVolatile(false);
650 AssertOK();
651}
652
653LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
654 Instruction *InsertBef)
655: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
656 Load, Ptr, Name, InsertBef) {
657 setVolatile(isVolatile);
658 AssertOK();
659}
660
661LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
662 BasicBlock *InsertAE)
663: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
664 Load, Ptr, Name, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000665 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000666 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000667}
668
669
670//===----------------------------------------------------------------------===//
671// StoreInst Implementation
672//===----------------------------------------------------------------------===//
673
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000674void StoreInst::AssertOK() {
675 assert(isa<PointerType>(getOperand(1)->getType()) &&
676 "Ptr must have pointer type!");
677 assert(getOperand(0)->getType() ==
678 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000679 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000680}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000682
683StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000684 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000685 Ops[0].init(val, this);
686 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000687 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688 AssertOK();
689}
690
691StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000692 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693 Ops[0].init(val, this);
694 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000695 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696 AssertOK();
697}
698
Misha Brukmanb1c93172005-04-21 23:48:37 +0000699StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000700 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000701 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000702 Ops[0].init(val, this);
703 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000704 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705 AssertOK();
706}
707
Misha Brukmanb1c93172005-04-21 23:48:37 +0000708StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000709 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000710 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711 Ops[0].init(val, this);
712 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000713 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000715}
716
717//===----------------------------------------------------------------------===//
718// GetElementPtrInst Implementation
719//===----------------------------------------------------------------------===//
720
721// checkType - Simple wrapper function to give a better assertion failure
722// message on bad indexes for a gep instruction.
723//
724static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000725 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000726 return Ty;
727}
728
Chris Lattner79807c3d2007-01-31 19:47:18 +0000729void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
730 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731 Use *OL = OperandList = new Use[NumOperands];
732 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733
Chris Lattner79807c3d2007-01-31 19:47:18 +0000734 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000735 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000736}
737
738void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000739 NumOperands = 3;
740 Use *OL = OperandList = new Use[3];
741 OL[0].init(Ptr, this);
742 OL[1].init(Idx0, this);
743 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000744}
745
Chris Lattner82981202005-05-03 05:43:30 +0000746void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
747 NumOperands = 2;
748 Use *OL = OperandList = new Use[2];
749 OL[0].init(Ptr, this);
750 OL[1].init(Idx, this);
751}
752
Chris Lattner79807c3d2007-01-31 19:47:18 +0000753
754GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
755 unsigned NumIdx,
756 const std::string &Name, Instruction *InBe)
757: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000758 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000759 GetElementPtr, 0, 0, Name, InBe) {
760 init(Ptr, Idx, NumIdx);
761}
762
763GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
764 unsigned NumIdx,
765 const std::string &Name, BasicBlock *IAE)
766: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000767 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000768 GetElementPtr, 0, 0, Name, IAE) {
769 init(Ptr, Idx, NumIdx);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000770}
771
Chris Lattner82981202005-05-03 05:43:30 +0000772GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
773 const std::string &Name, Instruction *InBe)
Reid Spencerdee14b52007-01-31 22:30:26 +0000774 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
775 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000776 GetElementPtr, 0, 0, Name, InBe) {
777 init(Ptr, Idx);
778}
779
780GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
781 const std::string &Name, BasicBlock *IAE)
Reid Spencerdee14b52007-01-31 22:30:26 +0000782 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
783 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000784 GetElementPtr, 0, 0, Name, IAE) {
785 init(Ptr, Idx);
786}
787
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000788GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
789 const std::string &Name, Instruction *InBe)
790 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
791 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793 init(Ptr, Idx0, Idx1);
794}
795
796GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000797 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
799 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000800 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000801 init(Ptr, Idx0, Idx1);
802}
803
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000804GetElementPtrInst::~GetElementPtrInst() {
805 delete[] OperandList;
806}
807
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000808// getIndexedType - Returns the type of the element that would be loaded with
809// a load instruction with the specified parameters.
810//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000811// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812// pointer type.
813//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000814const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000815 Value* const *Idxs,
816 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817 bool AllowCompositeLeaf) {
818 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
819
820 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000821 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000822 if (AllowCompositeLeaf ||
823 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
824 return cast<PointerType>(Ptr)->getElementType();
825 else
826 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000827
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828 unsigned CurIdx = 0;
829 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000830 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000831 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
832 return 0; // Can't load a whole structure or array!?!?
833 }
834
Chris Lattner302116a2007-01-31 04:40:28 +0000835 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000836 if (isa<PointerType>(CT) && CurIdx != 1)
837 return 0; // Can only index into pointer types at the first index!
838 if (!CT->indexValid(Index)) return 0;
839 Ptr = CT->getTypeAtIndex(Index);
840
841 // If the new type forwards to another type, then it is in the middle
842 // of being refined to another type (and hence, may have dropped all
843 // references to what it was using before). So, use the new forwarded
844 // type.
845 if (const Type * Ty = Ptr->getForwardedType()) {
846 Ptr = Ty;
847 }
848 }
Chris Lattner302116a2007-01-31 04:40:28 +0000849 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000850}
851
Misha Brukmanb1c93172005-04-21 23:48:37 +0000852const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000853 Value *Idx0, Value *Idx1,
854 bool AllowCompositeLeaf) {
855 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
856 if (!PTy) return 0; // Type isn't a pointer type!
857
858 // Check the pointer index.
859 if (!PTy->indexValid(Idx0)) return 0;
860
861 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
862 if (!CT || !CT->indexValid(Idx1)) return 0;
863
864 const Type *ElTy = CT->getTypeAtIndex(Idx1);
865 if (AllowCompositeLeaf || ElTy->isFirstClassType())
866 return ElTy;
867 return 0;
868}
869
Chris Lattner82981202005-05-03 05:43:30 +0000870const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
871 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
872 if (!PTy) return 0; // Type isn't a pointer type!
873
874 // Check the pointer index.
875 if (!PTy->indexValid(Idx)) return 0;
876
Chris Lattnerc2233332005-05-03 16:44:45 +0000877 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000878}
879
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000881// ExtractElementInst Implementation
882//===----------------------------------------------------------------------===//
883
884ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000885 const std::string &Name,
886 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000887 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +0000888 ExtractElement, Ops, 2, Name, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000889 assert(isValidOperands(Val, Index) &&
890 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000891 Ops[0].init(Val, this);
892 Ops[1].init(Index, this);
893}
894
Chris Lattner65511ff2006-10-05 06:24:58 +0000895ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
896 const std::string &Name,
897 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000898 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner65511ff2006-10-05 06:24:58 +0000899 ExtractElement, Ops, 2, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000900 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000901 assert(isValidOperands(Val, Index) &&
902 "Invalid extractelement instruction operands!");
903 Ops[0].init(Val, this);
904 Ops[1].init(Index, this);
905}
906
907
Robert Bocchino23004482006-01-10 19:05:34 +0000908ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000909 const std::string &Name,
910 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000911 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Robert Bocchino23004482006-01-10 19:05:34 +0000912 ExtractElement, Ops, 2, Name, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +0000913 assert(isValidOperands(Val, Index) &&
914 "Invalid extractelement instruction operands!");
915
Robert Bocchino23004482006-01-10 19:05:34 +0000916 Ops[0].init(Val, this);
917 Ops[1].init(Index, this);
918}
919
Chris Lattner65511ff2006-10-05 06:24:58 +0000920ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
921 const std::string &Name,
922 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000923 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner65511ff2006-10-05 06:24:58 +0000924 ExtractElement, Ops, 2, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000925 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000926 assert(isValidOperands(Val, Index) &&
927 "Invalid extractelement instruction operands!");
928
929 Ops[0].init(Val, this);
930 Ops[1].init(Index, this);
931}
932
933
Chris Lattner54865b32006-04-08 04:05:48 +0000934bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +0000935 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000936 return false;
937 return true;
938}
939
940
Robert Bocchino23004482006-01-10 19:05:34 +0000941//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +0000942// InsertElementInst Implementation
943//===----------------------------------------------------------------------===//
944
Chris Lattner0875d942006-04-14 22:20:32 +0000945InsertElementInst::InsertElementInst(const InsertElementInst &IE)
946 : Instruction(IE.getType(), InsertElement, Ops, 3) {
947 Ops[0].init(IE.Ops[0], this);
948 Ops[1].init(IE.Ops[1], this);
949 Ops[2].init(IE.Ops[2], this);
950}
Chris Lattner54865b32006-04-08 04:05:48 +0000951InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000952 const std::string &Name,
953 Instruction *InsertBef)
Chris Lattner54865b32006-04-08 04:05:48 +0000954 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
955 assert(isValidOperands(Vec, Elt, Index) &&
956 "Invalid insertelement instruction operands!");
957 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000958 Ops[1].init(Elt, this);
959 Ops[2].init(Index, this);
960}
961
Chris Lattner65511ff2006-10-05 06:24:58 +0000962InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
963 const std::string &Name,
964 Instruction *InsertBef)
965 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000966 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000967 assert(isValidOperands(Vec, Elt, Index) &&
968 "Invalid insertelement instruction operands!");
969 Ops[0].init(Vec, this);
970 Ops[1].init(Elt, this);
971 Ops[2].init(Index, this);
972}
973
974
Chris Lattner54865b32006-04-08 04:05:48 +0000975InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000976 const std::string &Name,
977 BasicBlock *InsertAE)
Chris Lattner54865b32006-04-08 04:05:48 +0000978 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
979 assert(isValidOperands(Vec, Elt, Index) &&
980 "Invalid insertelement instruction operands!");
981
982 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000983 Ops[1].init(Elt, this);
984 Ops[2].init(Index, this);
985}
986
Chris Lattner65511ff2006-10-05 06:24:58 +0000987InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
988 const std::string &Name,
989 BasicBlock *InsertAE)
990: Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000991 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000992 assert(isValidOperands(Vec, Elt, Index) &&
993 "Invalid insertelement instruction operands!");
994
995 Ops[0].init(Vec, this);
996 Ops[1].init(Elt, this);
997 Ops[2].init(Index, this);
998}
999
Chris Lattner54865b32006-04-08 04:05:48 +00001000bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1001 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001002 if (!isa<VectorType>(Vec->getType()))
Chris Lattner54865b32006-04-08 04:05:48 +00001003 return false; // First operand of insertelement must be packed type.
1004
Reid Spencerd84d35b2007-02-15 02:26:10 +00001005 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Chris Lattner54865b32006-04-08 04:05:48 +00001006 return false;// Second operand of insertelement must be packed element type.
1007
Reid Spencer8d9336d2006-12-31 05:26:44 +00001008 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001009 return false; // Third operand of insertelement must be uint.
1010 return true;
1011}
1012
1013
Robert Bocchinoca27f032006-01-17 20:07:22 +00001014//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001015// ShuffleVectorInst Implementation
1016//===----------------------------------------------------------------------===//
1017
Chris Lattner0875d942006-04-14 22:20:32 +00001018ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1019 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1020 Ops[0].init(SV.Ops[0], this);
1021 Ops[1].init(SV.Ops[1], this);
1022 Ops[2].init(SV.Ops[2], this);
1023}
1024
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001025ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1026 const std::string &Name,
1027 Instruction *InsertBefore)
1028 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) {
1029 assert(isValidOperands(V1, V2, Mask) &&
1030 "Invalid shuffle vector instruction operands!");
1031 Ops[0].init(V1, this);
1032 Ops[1].init(V2, this);
1033 Ops[2].init(Mask, this);
1034}
1035
1036ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1037 const std::string &Name,
1038 BasicBlock *InsertAtEnd)
1039 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) {
1040 assert(isValidOperands(V1, V2, Mask) &&
1041 "Invalid shuffle vector instruction operands!");
1042
1043 Ops[0].init(V1, this);
1044 Ops[1].init(V2, this);
1045 Ops[2].init(Mask, this);
1046}
1047
1048bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1049 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001050 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001051 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001052 if (!isa<VectorType>(Mask->getType()) ||
1053 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1054 cast<VectorType>(Mask->getType())->getNumElements() !=
1055 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001056 return false;
1057 return true;
1058}
1059
1060
1061//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001062// BinaryOperator Class
1063//===----------------------------------------------------------------------===//
1064
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001065void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001066{
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001067 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001068 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001069 assert(LHS->getType() == RHS->getType() &&
1070 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001071#ifndef NDEBUG
1072 switch (iType) {
1073 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001074 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001075 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001076 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001077 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001078 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001079 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001080 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001081 case UDiv:
1082 case SDiv:
1083 assert(getType() == LHS->getType() &&
1084 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001085 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1086 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001087 "Incorrect operand type (not integer) for S/UDIV");
1088 break;
1089 case FDiv:
1090 assert(getType() == LHS->getType() &&
1091 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001092 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1093 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001094 && "Incorrect operand type (not floating point) for FDIV");
1095 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001096 case URem:
1097 case SRem:
1098 assert(getType() == LHS->getType() &&
1099 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001100 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1101 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001102 "Incorrect operand type (not integer) for S/UREM");
1103 break;
1104 case FRem:
1105 assert(getType() == LHS->getType() &&
1106 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001107 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1108 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001109 && "Incorrect operand type (not floating point) for FREM");
1110 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001111 case Shl:
1112 case LShr:
1113 case AShr:
1114 assert(getType() == LHS->getType() &&
1115 "Shift operation should return same type as operands!");
1116 assert(getType()->isInteger() &&
1117 "Shift operation requires integer operands");
1118 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001119 case And: case Or:
1120 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001121 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001122 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001123 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001124 (isa<VectorType>(getType()) &&
1125 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001126 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001127 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001128 default:
1129 break;
1130 }
1131#endif
1132}
1133
1134BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001135 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001136 Instruction *InsertBefore) {
1137 assert(S1->getType() == S2->getType() &&
1138 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001139 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001140}
1141
1142BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001143 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001144 BasicBlock *InsertAtEnd) {
1145 BinaryOperator *Res = create(Op, S1, S2, Name);
1146 InsertAtEnd->getInstList().push_back(Res);
1147 return Res;
1148}
1149
1150BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1151 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001152 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1153 return new BinaryOperator(Instruction::Sub,
1154 zero, Op,
1155 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001156}
1157
1158BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1159 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001160 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1161 return new BinaryOperator(Instruction::Sub,
1162 zero, Op,
1163 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001164}
1165
1166BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1167 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001168 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001169 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001170 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001171 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001172 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001173 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001174 }
1175
1176 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001177 Op->getType(), Name, InsertBefore);
1178}
1179
1180BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1181 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001182 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001183 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001184 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001185 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001186 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001187 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001188 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001189 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001190 }
1191
1192 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193 Op->getType(), Name, InsertAtEnd);
1194}
1195
1196
1197// isConstantAllOnes - Helper function for several functions below
1198static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001199 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001200}
1201
1202bool BinaryOperator::isNeg(const Value *V) {
1203 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1204 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001205 return Bop->getOperand(0) ==
1206 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001207 return false;
1208}
1209
1210bool BinaryOperator::isNot(const Value *V) {
1211 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1212 return (Bop->getOpcode() == Instruction::Xor &&
1213 (isConstantAllOnes(Bop->getOperand(1)) ||
1214 isConstantAllOnes(Bop->getOperand(0))));
1215 return false;
1216}
1217
Chris Lattner2c7d1772005-04-24 07:28:37 +00001218Value *BinaryOperator::getNegArgument(Value *BinOp) {
1219 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1220 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001221}
1222
Chris Lattner2c7d1772005-04-24 07:28:37 +00001223const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1224 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001225}
1226
Chris Lattner2c7d1772005-04-24 07:28:37 +00001227Value *BinaryOperator::getNotArgument(Value *BinOp) {
1228 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1229 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1230 Value *Op0 = BO->getOperand(0);
1231 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001232 if (isConstantAllOnes(Op0)) return Op1;
1233
1234 assert(isConstantAllOnes(Op1));
1235 return Op0;
1236}
1237
Chris Lattner2c7d1772005-04-24 07:28:37 +00001238const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1239 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001240}
1241
1242
1243// swapOperands - Exchange the two operands to this instruction. This
1244// instruction is safe to use on any binary instruction and does not
1245// modify the semantics of the instruction. If the instruction is
1246// order dependent (SetLT f.e.) the opcode is changed.
1247//
1248bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001249 if (!isCommutative())
1250 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001251 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001252 return false;
1253}
1254
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001255//===----------------------------------------------------------------------===//
1256// CastInst Class
1257//===----------------------------------------------------------------------===//
1258
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001259// Just determine if this cast only deals with integral->integral conversion.
1260bool CastInst::isIntegerCast() const {
1261 switch (getOpcode()) {
1262 default: return false;
1263 case Instruction::ZExt:
1264 case Instruction::SExt:
1265 case Instruction::Trunc:
1266 return true;
1267 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001268 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001269 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001270}
1271
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001272bool CastInst::isLosslessCast() const {
1273 // Only BitCast can be lossless, exit fast if we're not BitCast
1274 if (getOpcode() != Instruction::BitCast)
1275 return false;
1276
1277 // Identity cast is always lossless
1278 const Type* SrcTy = getOperand(0)->getType();
1279 const Type* DstTy = getType();
1280 if (SrcTy == DstTy)
1281 return true;
1282
Reid Spencer8d9336d2006-12-31 05:26:44 +00001283 // Pointer to pointer is always lossless.
1284 if (isa<PointerType>(SrcTy))
1285 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001286 return false; // Other types have no identity values
1287}
1288
1289/// This function determines if the CastInst does not require any bits to be
1290/// changed in order to effect the cast. Essentially, it identifies cases where
1291/// no code gen is necessary for the cast, hence the name no-op cast. For
1292/// example, the following are all no-op casts:
1293/// # bitcast uint %X, int
1294/// # bitcast uint* %x, sbyte*
1295/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1296/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1297/// @brief Determine if a cast is a no-op.
1298bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1299 switch (getOpcode()) {
1300 default:
1301 assert(!"Invalid CastOp");
1302 case Instruction::Trunc:
1303 case Instruction::ZExt:
1304 case Instruction::SExt:
1305 case Instruction::FPTrunc:
1306 case Instruction::FPExt:
1307 case Instruction::UIToFP:
1308 case Instruction::SIToFP:
1309 case Instruction::FPToUI:
1310 case Instruction::FPToSI:
1311 return false; // These always modify bits
1312 case Instruction::BitCast:
1313 return true; // BitCast never modifies bits.
1314 case Instruction::PtrToInt:
1315 return IntPtrTy->getPrimitiveSizeInBits() ==
1316 getType()->getPrimitiveSizeInBits();
1317 case Instruction::IntToPtr:
1318 return IntPtrTy->getPrimitiveSizeInBits() ==
1319 getOperand(0)->getType()->getPrimitiveSizeInBits();
1320 }
1321}
1322
1323/// This function determines if a pair of casts can be eliminated and what
1324/// opcode should be used in the elimination. This assumes that there are two
1325/// instructions like this:
1326/// * %F = firstOpcode SrcTy %x to MidTy
1327/// * %S = secondOpcode MidTy %F to DstTy
1328/// The function returns a resultOpcode so these two casts can be replaced with:
1329/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1330/// If no such cast is permited, the function returns 0.
1331unsigned CastInst::isEliminableCastPair(
1332 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1333 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1334{
1335 // Define the 144 possibilities for these two cast instructions. The values
1336 // in this matrix determine what to do in a given situation and select the
1337 // case in the switch below. The rows correspond to firstOp, the columns
1338 // correspond to secondOp. In looking at the table below, keep in mind
1339 // the following cast properties:
1340 //
1341 // Size Compare Source Destination
1342 // Operator Src ? Size Type Sign Type Sign
1343 // -------- ------------ ------------------- ---------------------
1344 // TRUNC > Integer Any Integral Any
1345 // ZEXT < Integral Unsigned Integer Any
1346 // SEXT < Integral Signed Integer Any
1347 // FPTOUI n/a FloatPt n/a Integral Unsigned
1348 // FPTOSI n/a FloatPt n/a Integral Signed
1349 // UITOFP n/a Integral Unsigned FloatPt n/a
1350 // SITOFP n/a Integral Signed FloatPt n/a
1351 // FPTRUNC > FloatPt n/a FloatPt n/a
1352 // FPEXT < FloatPt n/a FloatPt n/a
1353 // PTRTOINT n/a Pointer n/a Integral Unsigned
1354 // INTTOPTR n/a Integral Unsigned Pointer n/a
1355 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001356 //
1357 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1358 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1359 // into "fptoui double to ulong", but this loses information about the range
1360 // of the produced value (we no longer know the top-part is all zeros).
1361 // Further this conversion is often much more expensive for typical hardware,
1362 // and causes issues when building libgcc. We disallow fptosi+sext for the
1363 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001364 const unsigned numCastOps =
1365 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1366 static const uint8_t CastResults[numCastOps][numCastOps] = {
1367 // T F F U S F F P I B -+
1368 // R Z S P P I I T P 2 N T |
1369 // U E E 2 2 2 2 R E I T C +- secondOp
1370 // N X X U S F F N X N 2 V |
1371 // C T T I I P P C T T P T -+
1372 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1373 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1374 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001375 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1376 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001377 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1378 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1379 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1380 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1381 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1382 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1383 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1384 };
1385
1386 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1387 [secondOp-Instruction::CastOpsBegin];
1388 switch (ElimCase) {
1389 case 0:
1390 // categorically disallowed
1391 return 0;
1392 case 1:
1393 // allowed, use first cast's opcode
1394 return firstOp;
1395 case 2:
1396 // allowed, use second cast's opcode
1397 return secondOp;
1398 case 3:
1399 // no-op cast in second op implies firstOp as long as the DestTy
1400 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001401 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001402 return firstOp;
1403 return 0;
1404 case 4:
1405 // no-op cast in second op implies firstOp as long as the DestTy
1406 // is floating point
1407 if (DstTy->isFloatingPoint())
1408 return firstOp;
1409 return 0;
1410 case 5:
1411 // no-op cast in first op implies secondOp as long as the SrcTy
1412 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001413 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001414 return secondOp;
1415 return 0;
1416 case 6:
1417 // no-op cast in first op implies secondOp as long as the SrcTy
1418 // is a floating point
1419 if (SrcTy->isFloatingPoint())
1420 return secondOp;
1421 return 0;
1422 case 7: {
1423 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1424 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1425 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1426 if (MidSize >= PtrSize)
1427 return Instruction::BitCast;
1428 return 0;
1429 }
1430 case 8: {
1431 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1432 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1433 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1434 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1435 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1436 if (SrcSize == DstSize)
1437 return Instruction::BitCast;
1438 else if (SrcSize < DstSize)
1439 return firstOp;
1440 return secondOp;
1441 }
1442 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1443 return Instruction::ZExt;
1444 case 10:
1445 // fpext followed by ftrunc is allowed if the bit size returned to is
1446 // the same as the original, in which case its just a bitcast
1447 if (SrcTy == DstTy)
1448 return Instruction::BitCast;
1449 return 0; // If the types are not the same we can't eliminate it.
1450 case 11:
1451 // bitcast followed by ptrtoint is allowed as long as the bitcast
1452 // is a pointer to pointer cast.
1453 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1454 return secondOp;
1455 return 0;
1456 case 12:
1457 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1458 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1459 return firstOp;
1460 return 0;
1461 case 13: {
1462 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1463 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1464 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1465 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1466 if (SrcSize <= PtrSize && SrcSize == DstSize)
1467 return Instruction::BitCast;
1468 return 0;
1469 }
1470 case 99:
1471 // cast combination can't happen (error in input). This is for all cases
1472 // where the MidTy is not the same for the two cast instructions.
1473 assert(!"Invalid Cast Combination");
1474 return 0;
1475 default:
1476 assert(!"Error in CastResults table!!!");
1477 return 0;
1478 }
1479 return 0;
1480}
1481
1482CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1483 const std::string &Name, Instruction *InsertBefore) {
1484 // Construct and return the appropriate CastInst subclass
1485 switch (op) {
1486 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1487 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1488 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1489 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1490 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1491 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1492 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1493 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1494 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1495 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1496 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1497 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1498 default:
1499 assert(!"Invalid opcode provided");
1500 }
1501 return 0;
1502}
1503
1504CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1505 const std::string &Name, BasicBlock *InsertAtEnd) {
1506 // Construct and return the appropriate CastInst subclass
1507 switch (op) {
1508 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1509 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1510 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1511 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1512 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1513 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1514 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1515 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1516 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1517 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1518 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1519 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1520 default:
1521 assert(!"Invalid opcode provided");
1522 }
1523 return 0;
1524}
1525
Reid Spencer5c140882006-12-04 20:17:56 +00001526CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1527 const std::string &Name,
1528 Instruction *InsertBefore) {
1529 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1530 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1531 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1532}
1533
1534CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1535 const std::string &Name,
1536 BasicBlock *InsertAtEnd) {
1537 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1538 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1539 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1540}
1541
1542CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1543 const std::string &Name,
1544 Instruction *InsertBefore) {
1545 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1546 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1547 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1548}
1549
1550CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1551 const std::string &Name,
1552 BasicBlock *InsertAtEnd) {
1553 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1554 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1555 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1556}
1557
1558CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1559 const std::string &Name,
1560 Instruction *InsertBefore) {
1561 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1562 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1563 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1564}
1565
1566CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1567 const std::string &Name,
1568 BasicBlock *InsertAtEnd) {
1569 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1570 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1571 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1572}
1573
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001574CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1575 const std::string &Name,
1576 BasicBlock *InsertAtEnd) {
1577 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001578 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001579 "Invalid cast");
1580
Chris Lattner03c49532007-01-15 02:27:26 +00001581 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001582 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1583 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1584}
1585
1586/// @brief Create a BitCast or a PtrToInt cast instruction
1587CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1588 const std::string &Name,
1589 Instruction *InsertBefore) {
1590 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001591 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001592 "Invalid cast");
1593
Chris Lattner03c49532007-01-15 02:27:26 +00001594 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001595 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1596 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1597}
1598
Reid Spencer7e933472006-12-12 00:49:44 +00001599CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1600 bool isSigned, const std::string &Name,
1601 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001602 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001603 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1604 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1605 Instruction::CastOps opcode =
1606 (SrcBits == DstBits ? Instruction::BitCast :
1607 (SrcBits > DstBits ? Instruction::Trunc :
1608 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1609 return create(opcode, C, Ty, Name, InsertBefore);
1610}
1611
1612CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1613 bool isSigned, const std::string &Name,
1614 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001615 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001616 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1617 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1618 Instruction::CastOps opcode =
1619 (SrcBits == DstBits ? Instruction::BitCast :
1620 (SrcBits > DstBits ? Instruction::Trunc :
1621 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1622 return create(opcode, C, Ty, Name, InsertAtEnd);
1623}
1624
1625CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1626 const std::string &Name,
1627 Instruction *InsertBefore) {
1628 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1629 "Invalid cast");
1630 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1631 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1632 Instruction::CastOps opcode =
1633 (SrcBits == DstBits ? Instruction::BitCast :
1634 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1635 return create(opcode, C, Ty, Name, InsertBefore);
1636}
1637
1638CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1639 const std::string &Name,
1640 BasicBlock *InsertAtEnd) {
1641 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1642 "Invalid cast");
1643 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1644 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1645 Instruction::CastOps opcode =
1646 (SrcBits == DstBits ? Instruction::BitCast :
1647 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1648 return create(opcode, C, Ty, Name, InsertAtEnd);
1649}
1650
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001651// Provide a way to get a "cast" where the cast opcode is inferred from the
1652// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001653// logic in the castIsValid function below. This axiom should hold:
1654// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1655// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001656// casting opcode for the arguments passed to it.
1657Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001658CastInst::getCastOpcode(
1659 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001660 // Get the bit sizes, we'll need these
1661 const Type *SrcTy = Src->getType();
1662 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1663 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1664
1665 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001666 if (DestTy->isInteger()) { // Casting to integral
1667 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001668 if (DestBits < SrcBits)
1669 return Trunc; // int -> smaller int
1670 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001671 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001672 return SExt; // signed -> SEXT
1673 else
1674 return ZExt; // unsigned -> ZEXT
1675 } else {
1676 return BitCast; // Same size, No-op cast
1677 }
1678 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001679 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001680 return FPToSI; // FP -> sint
1681 else
1682 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001683 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001684 assert(DestBits == PTy->getBitWidth() &&
1685 "Casting packed to integer of different width");
1686 return BitCast; // Same size, no-op cast
1687 } else {
1688 assert(isa<PointerType>(SrcTy) &&
1689 "Casting from a value that is not first-class type");
1690 return PtrToInt; // ptr -> int
1691 }
1692 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001693 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001694 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001695 return SIToFP; // sint -> FP
1696 else
1697 return UIToFP; // uint -> FP
1698 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1699 if (DestBits < SrcBits) {
1700 return FPTrunc; // FP -> smaller FP
1701 } else if (DestBits > SrcBits) {
1702 return FPExt; // FP -> larger FP
1703 } else {
1704 return BitCast; // same size, no-op cast
1705 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001706 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001707 assert(DestBits == PTy->getBitWidth() &&
1708 "Casting packed to floating point of different width");
1709 return BitCast; // same size, no-op cast
1710 } else {
1711 assert(0 && "Casting pointer or non-first class to float");
1712 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001713 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1714 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001715 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1716 "Casting packed to packed of different widths");
1717 return BitCast; // packed -> packed
1718 } else if (DestPTy->getBitWidth() == SrcBits) {
1719 return BitCast; // float/int -> packed
1720 } else {
1721 assert(!"Illegal cast to packed (wrong type or size)");
1722 }
1723 } else if (isa<PointerType>(DestTy)) {
1724 if (isa<PointerType>(SrcTy)) {
1725 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001726 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001727 return IntToPtr; // int -> ptr
1728 } else {
1729 assert(!"Casting pointer to other than pointer or int");
1730 }
1731 } else {
1732 assert(!"Casting to type that is not first-class");
1733 }
1734
1735 // If we fall through to here we probably hit an assertion cast above
1736 // and assertions are not turned on. Anything we return is an error, so
1737 // BitCast is as good a choice as any.
1738 return BitCast;
1739}
1740
1741//===----------------------------------------------------------------------===//
1742// CastInst SubClass Constructors
1743//===----------------------------------------------------------------------===//
1744
1745/// Check that the construction parameters for a CastInst are correct. This
1746/// could be broken out into the separate constructors but it is useful to have
1747/// it in one place and to eliminate the redundant code for getting the sizes
1748/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001749bool
1750CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001751
1752 // Check for type sanity on the arguments
1753 const Type *SrcTy = S->getType();
1754 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1755 return false;
1756
1757 // Get the size of the types in bits, we'll need this later
1758 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1759 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1760
1761 // Switch on the opcode provided
1762 switch (op) {
1763 default: return false; // This is an input error
1764 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001765 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001766 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001767 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001768 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001769 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001770 case Instruction::FPTrunc:
1771 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1772 SrcBitSize > DstBitSize;
1773 case Instruction::FPExt:
1774 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1775 SrcBitSize < DstBitSize;
1776 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001777 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001778 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001779 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001780 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001781 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001782 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001783 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001784 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001785 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001786 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001787 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001788 case Instruction::BitCast:
1789 // BitCast implies a no-op cast of type only. No bits change.
1790 // However, you can't cast pointers to anything but pointers.
1791 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1792 return false;
1793
1794 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1795 // these cases, the cast is okay if the source and destination bit widths
1796 // are identical.
1797 return SrcBitSize == DstBitSize;
1798 }
1799}
1800
1801TruncInst::TruncInst(
1802 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1803) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001804 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001805}
1806
1807TruncInst::TruncInst(
1808 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1809) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001810 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001811}
1812
1813ZExtInst::ZExtInst(
1814 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1815) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001816 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001817}
1818
1819ZExtInst::ZExtInst(
1820 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1821) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001822 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001823}
1824SExtInst::SExtInst(
1825 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1826) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001827 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001828}
1829
Jeff Cohencc08c832006-12-02 02:22:01 +00001830SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001831 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1832) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001833 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001834}
1835
1836FPTruncInst::FPTruncInst(
1837 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1838) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001839 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001840}
1841
1842FPTruncInst::FPTruncInst(
1843 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1844) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001845 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846}
1847
1848FPExtInst::FPExtInst(
1849 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1850) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001851 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001852}
1853
1854FPExtInst::FPExtInst(
1855 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1856) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001857 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858}
1859
1860UIToFPInst::UIToFPInst(
1861 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1862) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001863 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001864}
1865
1866UIToFPInst::UIToFPInst(
1867 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1868) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001869 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001870}
1871
1872SIToFPInst::SIToFPInst(
1873 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1874) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001875 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001876}
1877
1878SIToFPInst::SIToFPInst(
1879 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1880) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001881 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001882}
1883
1884FPToUIInst::FPToUIInst(
1885 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1886) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001887 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001888}
1889
1890FPToUIInst::FPToUIInst(
1891 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1892) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001893 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001894}
1895
1896FPToSIInst::FPToSIInst(
1897 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1898) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001899 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001900}
1901
1902FPToSIInst::FPToSIInst(
1903 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1904) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001905 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001906}
1907
1908PtrToIntInst::PtrToIntInst(
1909 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1910) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001911 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001912}
1913
1914PtrToIntInst::PtrToIntInst(
1915 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1916) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001917 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001918}
1919
1920IntToPtrInst::IntToPtrInst(
1921 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1922) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001923 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001924}
1925
1926IntToPtrInst::IntToPtrInst(
1927 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1928) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001929 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001930}
1931
1932BitCastInst::BitCastInst(
1933 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1934) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001935 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936}
1937
1938BitCastInst::BitCastInst(
1939 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1940) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001941 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001942}
Chris Lattnerf16dc002006-09-17 19:29:56 +00001943
1944//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00001945// CmpInst Classes
1946//===----------------------------------------------------------------------===//
1947
1948CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1949 const std::string &Name, Instruction *InsertBefore)
Reid Spencer542964f2007-01-11 18:21:29 +00001950 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001951 Ops[0].init(LHS, this);
1952 Ops[1].init(RHS, this);
1953 SubclassData = predicate;
1954 if (op == Instruction::ICmp) {
1955 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1956 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1957 "Invalid ICmp predicate value");
1958 const Type* Op0Ty = getOperand(0)->getType();
1959 const Type* Op1Ty = getOperand(1)->getType();
1960 assert(Op0Ty == Op1Ty &&
1961 "Both operands to ICmp instruction are not of the same type!");
1962 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001963 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001964 "Invalid operand types for ICmp instruction");
1965 return;
1966 }
1967 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1968 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1969 "Invalid FCmp predicate value");
1970 const Type* Op0Ty = getOperand(0)->getType();
1971 const Type* Op1Ty = getOperand(1)->getType();
1972 assert(Op0Ty == Op1Ty &&
1973 "Both operands to FCmp instruction are not of the same type!");
1974 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001975 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001976 "Invalid operand types for FCmp instruction");
1977}
1978
1979CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1980 const std::string &Name, BasicBlock *InsertAtEnd)
Reid Spencer542964f2007-01-11 18:21:29 +00001981 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001982 Ops[0].init(LHS, this);
1983 Ops[1].init(RHS, this);
1984 SubclassData = predicate;
1985 if (op == Instruction::ICmp) {
1986 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1987 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1988 "Invalid ICmp predicate value");
1989
1990 const Type* Op0Ty = getOperand(0)->getType();
1991 const Type* Op1Ty = getOperand(1)->getType();
1992 assert(Op0Ty == Op1Ty &&
1993 "Both operands to ICmp instruction are not of the same type!");
1994 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001995 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001996 "Invalid operand types for ICmp instruction");
1997 return;
1998 }
1999 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2000 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2001 "Invalid FCmp predicate value");
2002 const Type* Op0Ty = getOperand(0)->getType();
2003 const Type* Op1Ty = getOperand(1)->getType();
2004 assert(Op0Ty == Op1Ty &&
2005 "Both operands to FCmp instruction are not of the same type!");
2006 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002007 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002008 "Invalid operand types for FCmp instruction");
2009}
2010
2011CmpInst *
2012CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2013 const std::string &Name, Instruction *InsertBefore) {
2014 if (Op == Instruction::ICmp) {
2015 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2016 InsertBefore);
2017 }
2018 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2019 InsertBefore);
2020}
2021
2022CmpInst *
2023CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2024 const std::string &Name, BasicBlock *InsertAtEnd) {
2025 if (Op == Instruction::ICmp) {
2026 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2027 InsertAtEnd);
2028 }
2029 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2030 InsertAtEnd);
2031}
2032
2033void CmpInst::swapOperands() {
2034 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2035 IC->swapOperands();
2036 else
2037 cast<FCmpInst>(this)->swapOperands();
2038}
2039
2040bool CmpInst::isCommutative() {
2041 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2042 return IC->isCommutative();
2043 return cast<FCmpInst>(this)->isCommutative();
2044}
2045
2046bool CmpInst::isEquality() {
2047 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2048 return IC->isEquality();
2049 return cast<FCmpInst>(this)->isEquality();
2050}
2051
2052
2053ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2054 switch (pred) {
2055 default:
2056 assert(!"Unknown icmp predicate!");
2057 case ICMP_EQ: return ICMP_NE;
2058 case ICMP_NE: return ICMP_EQ;
2059 case ICMP_UGT: return ICMP_ULE;
2060 case ICMP_ULT: return ICMP_UGE;
2061 case ICMP_UGE: return ICMP_ULT;
2062 case ICMP_ULE: return ICMP_UGT;
2063 case ICMP_SGT: return ICMP_SLE;
2064 case ICMP_SLT: return ICMP_SGE;
2065 case ICMP_SGE: return ICMP_SLT;
2066 case ICMP_SLE: return ICMP_SGT;
2067 }
2068}
2069
2070ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2071 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002072 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002073 case ICMP_EQ: case ICMP_NE:
2074 return pred;
2075 case ICMP_SGT: return ICMP_SLT;
2076 case ICMP_SLT: return ICMP_SGT;
2077 case ICMP_SGE: return ICMP_SLE;
2078 case ICMP_SLE: return ICMP_SGE;
2079 case ICMP_UGT: return ICMP_ULT;
2080 case ICMP_ULT: return ICMP_UGT;
2081 case ICMP_UGE: return ICMP_ULE;
2082 case ICMP_ULE: return ICMP_UGE;
2083 }
2084}
2085
Reid Spencer266e42b2006-12-23 06:05:41 +00002086ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2087 switch (pred) {
2088 default: assert(! "Unknown icmp predicate!");
2089 case ICMP_EQ: case ICMP_NE:
2090 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2091 return pred;
2092 case ICMP_UGT: return ICMP_SGT;
2093 case ICMP_ULT: return ICMP_SLT;
2094 case ICMP_UGE: return ICMP_SGE;
2095 case ICMP_ULE: return ICMP_SLE;
2096 }
2097}
2098
2099bool ICmpInst::isSignedPredicate(Predicate pred) {
2100 switch (pred) {
2101 default: assert(! "Unknown icmp predicate!");
2102 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2103 return true;
2104 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2105 case ICMP_UGE: case ICMP_ULE:
2106 return false;
2107 }
2108}
2109
Reid Spencerd9436b62006-11-20 01:22:35 +00002110FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2111 switch (pred) {
2112 default:
2113 assert(!"Unknown icmp predicate!");
2114 case FCMP_OEQ: return FCMP_UNE;
2115 case FCMP_ONE: return FCMP_UEQ;
2116 case FCMP_OGT: return FCMP_ULE;
2117 case FCMP_OLT: return FCMP_UGE;
2118 case FCMP_OGE: return FCMP_ULT;
2119 case FCMP_OLE: return FCMP_UGT;
2120 case FCMP_UEQ: return FCMP_ONE;
2121 case FCMP_UNE: return FCMP_OEQ;
2122 case FCMP_UGT: return FCMP_OLE;
2123 case FCMP_ULT: return FCMP_OGE;
2124 case FCMP_UGE: return FCMP_OLT;
2125 case FCMP_ULE: return FCMP_OGT;
2126 case FCMP_ORD: return FCMP_UNO;
2127 case FCMP_UNO: return FCMP_ORD;
2128 case FCMP_TRUE: return FCMP_FALSE;
2129 case FCMP_FALSE: return FCMP_TRUE;
2130 }
2131}
2132
2133FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2134 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002135 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002136 case FCMP_FALSE: case FCMP_TRUE:
2137 case FCMP_OEQ: case FCMP_ONE:
2138 case FCMP_UEQ: case FCMP_UNE:
2139 case FCMP_ORD: case FCMP_UNO:
2140 return pred;
2141 case FCMP_OGT: return FCMP_OLT;
2142 case FCMP_OLT: return FCMP_OGT;
2143 case FCMP_OGE: return FCMP_OLE;
2144 case FCMP_OLE: return FCMP_OGE;
2145 case FCMP_UGT: return FCMP_ULT;
2146 case FCMP_ULT: return FCMP_UGT;
2147 case FCMP_UGE: return FCMP_ULE;
2148 case FCMP_ULE: return FCMP_UGE;
2149 }
2150}
2151
Reid Spencer266e42b2006-12-23 06:05:41 +00002152bool CmpInst::isUnsigned(unsigned short predicate) {
2153 switch (predicate) {
2154 default: return false;
2155 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2156 case ICmpInst::ICMP_UGE: return true;
2157 }
2158}
2159
2160bool CmpInst::isSigned(unsigned short predicate){
2161 switch (predicate) {
2162 default: return false;
2163 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2164 case ICmpInst::ICMP_SGE: return true;
2165 }
2166}
2167
2168bool CmpInst::isOrdered(unsigned short predicate) {
2169 switch (predicate) {
2170 default: return false;
2171 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2172 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2173 case FCmpInst::FCMP_ORD: return true;
2174 }
2175}
2176
2177bool CmpInst::isUnordered(unsigned short predicate) {
2178 switch (predicate) {
2179 default: return false;
2180 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2181 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2182 case FCmpInst::FCMP_UNO: return true;
2183 }
2184}
2185
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002186//===----------------------------------------------------------------------===//
2187// SwitchInst Implementation
2188//===----------------------------------------------------------------------===//
2189
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002190void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002191 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002192 ReservedSpace = 2+NumCases*2;
2193 NumOperands = 2;
2194 OperandList = new Use[ReservedSpace];
2195
2196 OperandList[0].init(Value, this);
2197 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002198}
2199
Misha Brukmanb1c93172005-04-21 23:48:37 +00002200SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002201 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
2202 SI.getNumOperands()) {
2203 Use *OL = OperandList, *InOL = SI.OperandList;
2204 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2205 OL[i].init(InOL[i], this);
2206 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002207 }
2208}
2209
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002210SwitchInst::~SwitchInst() {
2211 delete [] OperandList;
2212}
2213
2214
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002215/// addCase - Add an entry to the switch instruction...
2216///
Chris Lattner47ac1872005-02-24 05:32:09 +00002217void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002218 unsigned OpNo = NumOperands;
2219 if (OpNo+2 > ReservedSpace)
2220 resizeOperands(0); // Get more space!
2221 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002222 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002223 NumOperands = OpNo+2;
2224 OperandList[OpNo].init(OnVal, this);
2225 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002226}
2227
2228/// removeCase - This method removes the specified successor from the switch
2229/// instruction. Note that this cannot be used to remove the default
2230/// destination (successor #0).
2231///
2232void SwitchInst::removeCase(unsigned idx) {
2233 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002234 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2235
2236 unsigned NumOps = getNumOperands();
2237 Use *OL = OperandList;
2238
2239 // Move everything after this operand down.
2240 //
2241 // FIXME: we could just swap with the end of the list, then erase. However,
2242 // client might not expect this to happen. The code as it is thrashes the
2243 // use/def lists, which is kinda lame.
2244 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2245 OL[i-2] = OL[i];
2246 OL[i-2+1] = OL[i+1];
2247 }
2248
2249 // Nuke the last value.
2250 OL[NumOps-2].set(0);
2251 OL[NumOps-2+1].set(0);
2252 NumOperands = NumOps-2;
2253}
2254
2255/// resizeOperands - resize operands - This adjusts the length of the operands
2256/// list according to the following behavior:
2257/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2258/// of operation. This grows the number of ops by 1.5 times.
2259/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2260/// 3. If NumOps == NumOperands, trim the reserved space.
2261///
2262void SwitchInst::resizeOperands(unsigned NumOps) {
2263 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002264 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002265 } else if (NumOps*2 > NumOperands) {
2266 // No resize needed.
2267 if (ReservedSpace >= NumOps) return;
2268 } else if (NumOps == NumOperands) {
2269 if (ReservedSpace == NumOps) return;
2270 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002271 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002272 }
2273
2274 ReservedSpace = NumOps;
2275 Use *NewOps = new Use[NumOps];
2276 Use *OldOps = OperandList;
2277 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2278 NewOps[i].init(OldOps[i], this);
2279 OldOps[i].set(0);
2280 }
2281 delete [] OldOps;
2282 OperandList = NewOps;
2283}
2284
2285
2286BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2287 return getSuccessor(idx);
2288}
2289unsigned SwitchInst::getNumSuccessorsV() const {
2290 return getNumSuccessors();
2291}
2292void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2293 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002294}
Chris Lattnerf22be932004-10-15 23:52:53 +00002295
2296
2297// Define these methods here so vtables don't get emitted into every translation
2298// unit that uses these classes.
2299
2300GetElementPtrInst *GetElementPtrInst::clone() const {
2301 return new GetElementPtrInst(*this);
2302}
2303
2304BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002305 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002306}
2307
Reid Spencerd9436b62006-11-20 01:22:35 +00002308CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002309 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002310}
2311
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2313AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2314FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2315LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2316StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2317CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2318CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2319CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2320CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2321CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2322CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2323CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2324CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2325CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2326CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2327CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2328CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2329CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2331VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2332
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002333ExtractElementInst *ExtractElementInst::clone() const {
2334 return new ExtractElementInst(*this);
2335}
2336InsertElementInst *InsertElementInst::clone() const {
2337 return new InsertElementInst(*this);
2338}
2339ShuffleVectorInst *ShuffleVectorInst::clone() const {
2340 return new ShuffleVectorInst(*this);
2341}
Chris Lattnerf22be932004-10-15 23:52:53 +00002342PHINode *PHINode::clone() const { return new PHINode(*this); }
2343ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2344BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2345SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2346InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2347UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002348UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}